forked from ScottOaks/JavaPerformanceTuning
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStockPriceCreateJDBC.java
More file actions
160 lines (152 loc) · 6.44 KB
/
Copy pathStockPriceCreateJDBC.java
File metadata and controls
160 lines (152 loc) · 6.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
/*
* Copyright (c) 2013,2014 Scott Oaks. All rights reserved.
*/
package net.sdo;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Locale;
import java.util.concurrent.TimeUnit;
import javax.persistence.EntityManager;
import net.sdo.stock.StockPrice;
import net.sdo.stock.StockPriceUtils;
import net.sdo.stockimpl.MockStockPriceEntityManagerFactory;
import net.sdo.stockimpl.StockPriceEagerLazyImpl;
import net.sdo.stockimpl.StockPricePK;
import oracle.jdbc.OracleConnection;
public class StockPriceCreateJDBC extends Thread {
private static final NumberFormat nf = NumberFormat.getCurrencyInstance(Locale.US);
private static long msPerDay = TimeUnit.MILLISECONDS.convert(1, TimeUnit.DAYS);
private static DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, Locale.US);
private static Date startDate;
private static Date endDate;
private static EntityManager mockEM =
new MockStockPriceEntityManagerFactory("MockEntityManager").
createEntityManager();
private static int nTransactions = 1;
private static String URL;
private static String user;
private static String pw;
private static boolean autocommit;
private static boolean batch;
private static String insertStockSQL = "INSERT INTO STOCKPRICE("
+ "CLOSINGPRICE, HIGH, ISYEARHIGH, ISYEARLOW, LOW, OPENINGPRICE, PRICEDATE, SYMBOL) "
+ "VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
private static String insertOptionSQL = "INSERT INTO STOCKOPTIONPRICE("
+ "PRICE, EXPIRATIONPERIOD, PRICEDATE, SYMBOL) "
+ "VALUES (?, ?, ?, ?)";
private int startStock;
private int numStocks;
public StockPriceCreateJDBC(int start, int num) {
startStock = start;
numStocks = num;
}
@Override
public void run() {
System.out.println(Thread.currentThread() + ": Starting");
try (Connection c = DriverManager.getConnection(URL, user, pw)) {
c.setAutoCommit(autocommit);
((OracleConnection) c).setImplicitCachingEnabled(true);
((OracleConnection) c).setStatementCacheSize(10);
try (PreparedStatement ps = c.prepareStatement(insertStockSQL);
PreparedStatement ps2 = c.prepareStatement(insertOptionSQL)) {
for (int i = startStock; i < numStocks; i++) {
String symbol = StockPriceUtils.makeSymbol(i);
Date curDate = new Date(startDate.getTime());
System.out.println(Thread.currentThread() +
": Processing " + i + ": " + symbol);
while (!curDate.after(endDate)) {
// MockEM creates the data to store
StockPrice sp =
mockEM.find(StockPriceEagerLazyImpl.class,
new StockPricePK(symbol, (Date) curDate.clone()));
if (sp != null) {
ps.clearParameters();
ps.setBigDecimal(1, sp.getClosingPrice());
ps.setBigDecimal(2, sp.getHigh());
ps.setInt(3, sp.isYearHigh() ? 1 : 0);
ps.setInt(4, sp.isYearLow() ? 1 : 0);
ps.setBigDecimal(5, sp.getLow());
ps.setBigDecimal(6, sp.getOpeningPrice());
ps.setDate(7, new java.sql.Date(sp.getDate().getTime()));
ps.setString(8, sp.getSymbol());
if (batch) {
ps.addBatch();
} else {
ps.executeUpdate();
}
for (int j = 0; j < 5; j++) {
ps2.clearParameters();
ps2.setBigDecimal(1,
sp.getClosingPrice().multiply(
new BigDecimal(1 + j / 100.)));
ps2.setInt(2, j);
ps2.setDate(3,
new java.sql.Date(sp.getDate().getTime()));
ps2.setString(4, sp.getSymbol());
if (batch) {
ps2.addBatch();
} else {
ps2.executeUpdate();
}
}
}
curDate.setTime(curDate.getTime() + msPerDay);
}
if (batch && (i % nTransactions) == 0) {
ps.executeBatch();
ps2.executeBatch();
}
if (!autocommit && (i % nTransactions) == 0) {
c.commit();
}
}
if (batch) {
ps.executeBatch();
ps2.executeBatch();
}
if (!autocommit) {
c.commit();
}
}
} catch (SQLException sqe) {
sqe.printStackTrace();
}
System.out.println(Thread.currentThread() + ": Complete");
}
public static void main(String[] args) throws ParseException {
int curArg = 0;
URL = args[curArg++];
user = args[curArg++];
pw = args[curArg++];
int nThreads = Integer.parseInt(args[curArg++]);
int num = Integer.parseInt(args[curArg++]);
int numPerThread = num / nThreads;
startDate = df.parse(args[curArg++]);
endDate = df.parse(args[curArg++]);
autocommit = Boolean.parseBoolean(args[curArg++]);
nTransactions = Integer.parseInt(args[curArg++]);
batch = Boolean.parseBoolean(args[curArg++]);
StockPriceCreateJDBC[] threads = new StockPriceCreateJDBC[nThreads];
for (int i = 0; i < nThreads; i++) {
threads[i] = new StockPriceCreateJDBC(numPerThread * i,
numPerThread * i + numPerThread);
}
for (int i = 0; i < nThreads; i++) {
threads[i].start();
}
for (int i = 0; i < nThreads; i++) {
try {
threads[i].join();
} catch (InterruptedException ex) {
System.err.println("Thread join interrupted");
}
}
}
}