Skip to content

Commit bf1032b

Browse files
committed
2 parents a44dc44 + 8bbde83 commit bf1032b

File tree

12 files changed

+876
-0
lines changed

12 files changed

+876
-0
lines changed

java/jdbc/ConnectionSamples/DataSourceSample.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ public class DataSourceSample {
3838
// The recommended format of a connection URL is the long format with the
3939
// connection descriptor.
4040
final static String DB_URL= "jdbc:oracle:thin:@myhost:1521/myorcldbservicename";
41+
// For ATP and ADW - use the TNS Alias name along with the TNS_ADMIN
42+
// final static String DB_URL="jdbc:oracle:thin:@myhost:1521@wallet_dbname?TNS_ADMIN=/Users/test/wallet_dbname";
4143
final static String DB_USER = "hr";
4244
final static String DB_PASSWORD = "hr";
4345

java/jdbc/ConnectionSamples/UCPSample.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ The code sample demonstrates Universal Connection Pool (UCP) as a client
3131

3232
public class UCPSample {
3333
final static String DB_URL="jdbc:oracle:thin:@myhost:1521/orclservicename";
34+
// Use the TNS Alias name along with the TNS_ADMIN - For ATP and ADW
35+
// final static String DB_URL="jdbc:oracle:thin:@myhost:1521@wallet_dbname?TNS_ADMIN=/Users/test/wallet_dbname";
3436
final static String DB_USER = "hr";
3537
final static String DB_PASSWORD = "hr";
3638
final static String CONN_FACTORY_CLASS_NAME="oracle.jdbc.pool.OracleDataSource";
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
hrPool: {
3+
user: process.env.HR_USER,
4+
password: process.env.HR_PASSWORD,
5+
connectString: process.env.HR_CONNECTIONSTRING,
6+
poolMin: 10,
7+
poolMax: 10,
8+
poolIncrement: 0
9+
}
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module.exports = {
2+
port: process.env.HTTP_PORT || 3000
3+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
const employees = require('../db_apis/employees.js');
2+
3+
async function get(req, res, next) {
4+
try {
5+
const context = {};
6+
7+
context.id = parseInt(req.params.id, 10);
8+
context.skip = req.query.skip;
9+
context.limit = req.query.limit;
10+
context.sort = req.query.sort;
11+
context.filter = req.query.filter;
12+
13+
const result = await employees.find(context);
14+
15+
if (req.params.id) {
16+
if (result.items.length === 1) {
17+
res.status(200).json(result.items[0]);
18+
} else {
19+
res.status(404).end();
20+
}
21+
} else {
22+
res.status(200).json(result);
23+
}
24+
} catch (err) {
25+
next(err);
26+
}
27+
}
28+
29+
module.exports.get = get;
30+
31+
function getEmployeeFromRec(req) {
32+
const employee = {
33+
first_name: req.body.first_name,
34+
last_name: req.body.last_name,
35+
email: req.body.email,
36+
phone_number: req.body.phone_number,
37+
hire_date: req.body.hire_date,
38+
job_id: req.body.job_id,
39+
salary: req.body.salary,
40+
commission_pct: req.body.commission_pct,
41+
manager_id: req.body.manager_id,
42+
department_id: req.body.department_id
43+
};
44+
45+
return employee;
46+
}
47+
48+
async function post(req, res, next) {
49+
try {
50+
let employee = getEmployeeFromRec(req);
51+
52+
employee = await employees.create(employee);
53+
54+
res.status(201).json(employee);
55+
} catch (err) {
56+
next(err);
57+
}
58+
}
59+
60+
module.exports.post = post;
61+
62+
async function put(req, res, next) {
63+
try {
64+
let employee = getEmployeeFromRec(req);
65+
66+
employee.employee_id = parseInt(req.params.id, 10);
67+
68+
employee = await employees.update(employee);
69+
70+
if (employee !== null) {
71+
res.status(200).json(employee);
72+
} else {
73+
res.status(404).end();
74+
}
75+
} catch (err) {
76+
next(err);
77+
}
78+
}
79+
80+
module.exports.put = put;
81+
82+
async function del(req, res, next) {
83+
try {
84+
const id = parseInt(req.params.id, 10);
85+
86+
const success = await employees.delete(id);
87+
88+
if (success) {
89+
res.status(204).end();
90+
} else {
91+
res.status(404).end();
92+
}
93+
} catch (err) {
94+
next(err);
95+
}
96+
}
97+
98+
module.exports.delete = del;
99+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
const oracledb = require('oracledb');
2+
const queryWrap = require('query-wrap');
3+
const database = require('../services/database.js');
4+
5+
const baseQuery =
6+
`select employee_id "id",
7+
first_name "first_name",
8+
last_name "last_name",
9+
email "email",
10+
phone_number "phone_number",
11+
hire_date "hire_date",
12+
job_id "job_id",
13+
salary "salary",
14+
commission_pct "commission_pct",
15+
manager_id "manager_id",
16+
department_id "department_id"
17+
from employees
18+
where 1 = 1`;
19+
20+
const sortableColumns = ['id', 'last_name', 'email', 'hire_date', 'salary'];
21+
22+
async function find(context) {
23+
const sort = context.sort || [{column: 'id'}];
24+
let filter = context.filter;
25+
26+
if (context.id) {
27+
filter = {id: context.id};
28+
}
29+
30+
const result = await queryWrap.execute(
31+
baseQuery,
32+
[],
33+
{
34+
skip: context.skip,
35+
limit: context.limit,
36+
sort: sort,
37+
filter: filter
38+
}
39+
);
40+
41+
return result;
42+
}
43+
44+
module.exports.find = find;
45+
46+
const createSql =
47+
`insert into employees (
48+
first_name,
49+
last_name,
50+
email,
51+
phone_number,
52+
hire_date,
53+
job_id,
54+
salary,
55+
commission_pct,
56+
manager_id,
57+
department_id
58+
) values (
59+
:first_name,
60+
:last_name,
61+
:email,
62+
:phone_number,
63+
:hire_date,
64+
:job_id,
65+
:salary,
66+
:commission_pct,
67+
:manager_id,
68+
:department_id
69+
) returning employee_id
70+
into :employee_id`;
71+
72+
async function create(emp) {
73+
const employee = Object.assign({}, emp);
74+
75+
employee.employee_id = {
76+
dir: oracledb.BIND_OUT,
77+
type: oracledb.NUMBER
78+
}
79+
80+
const result = await database.simpleExecute(createSql, employee);
81+
82+
employee.employee_id = result.outBinds.employee_id[0];
83+
84+
return employee;
85+
}
86+
87+
module.exports.create = create;
88+
89+
const updateSql =
90+
`update employees
91+
set first_name = :first_name,
92+
last_name = :last_name,
93+
email = :email,
94+
phone_number = :phone_number,
95+
hire_date = :hire_date,
96+
job_id = :job_id,
97+
salary = :salary,
98+
commission_pct = :commission_pct,
99+
manager_id = :manager_id,
100+
department_id = :department_id
101+
where employee_id = :employee_id`;
102+
103+
async function update(emp) {
104+
const employee = Object.assign({}, emp);
105+
const result = await database.simpleExecute(updateSql, employee);
106+
107+
if (result.rowsAffected && result.rowsAffected === 1) {
108+
return employee;
109+
} else {
110+
return null;
111+
}
112+
}
113+
114+
module.exports.update = update;
115+
116+
const deleteSql =
117+
`begin
118+
119+
delete from job_history
120+
where employee_id = :employee_id;
121+
122+
delete from employees
123+
where employee_id = :employee_id;
124+
125+
:rowcount := sql%rowcount;
126+
127+
end;`
128+
129+
async function del(id) {
130+
const binds = {
131+
employee_id: id,
132+
rowcount: {
133+
dir: oracledb.BIND_OUT,
134+
type: oracledb.NUMBER
135+
}
136+
}
137+
const result = await database.simpleExecute(deleteSql, binds);
138+
139+
return result.outBinds.rowcount === 1;
140+
}
141+
142+
module.exports.delete = del;
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
const webServer = require('./services/web-server.js');
2+
const database = require('./services/database.js');
3+
const dbConfig = require('./config/database.js');
4+
const defaultThreadPoolSize = 4;
5+
6+
// Increase thread pool size by poolMax
7+
process.env.UV_THREADPOOL_SIZE = dbConfig.hrPool.poolMax + defaultThreadPoolSize;
8+
9+
async function startup() {
10+
console.log('Starting application');
11+
12+
try {
13+
console.log('Initializing database module');
14+
15+
await database.initialize();
16+
} catch (err) {
17+
console.error(err);
18+
19+
process.exit(1); // Non-zero failure code
20+
}
21+
22+
try {
23+
console.log('Initializing web server module');
24+
25+
await webServer.initialize();
26+
} catch (err) {
27+
console.error(err);
28+
29+
process.exit(1); // Non-zero failure code
30+
}
31+
}
32+
33+
startup();
34+
35+
async function shutdown(e) {
36+
let err = e;
37+
38+
console.log('Shutting down application');
39+
40+
try {
41+
console.log('Closing web server module');
42+
43+
await webServer.close();
44+
} catch (e) {
45+
console.error(e);
46+
47+
err = err || e;
48+
}
49+
50+
try {
51+
console.log('Closing database module');
52+
53+
await database.close();
54+
} catch (e) {
55+
console.error(e);
56+
57+
err = err || e;
58+
}
59+
60+
console.log('Exiting process');
61+
62+
if (err) {
63+
process.exit(1); // Non-zero failure code
64+
} else {
65+
process.exit(0);
66+
}
67+
}
68+
69+
process.on('SIGTERM', () => {
70+
console.log('Received SIGTERM');
71+
72+
shutdown();
73+
});
74+
75+
process.on('SIGINT', () => {
76+
console.log('Received SIGINT');
77+
78+
shutdown();
79+
});
80+
81+
process.on('uncaughtException', err => {
82+
console.log('Uncaught exception');
83+
console.error(err);
84+
85+
shutdown(err);
86+
});

0 commit comments

Comments
 (0)