OmniSciDB  04ee39c94c
Worker.java
Go to the documentation of this file.
1 
2 /*
3  * Copyright 2017 MapD Technologies, Inc.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20 
21 import java.sql.Connection;
22 import java.sql.DriverManager;
23 import java.sql.ResultSet;
24 import java.sql.SQLException;
25 import java.sql.Statement;
26 import java.util.Random;
27 
28 class Worker implements Runnable {
29  final static Logger MAPDLOGGER = LoggerFactory.getLogger(Worker.class);
30  // JDBC driver name and database URL
31  static final String JDBC_DRIVER = "com.omnisci.jdbc.OmniSciDriver";
32  static final String DB_URL = "jdbc:omnisci:localhost:6274:omnisci";
33 
34  // Database credentials
35  static final String USER = "admin";
36  static final String PASS = "HyperInteractive";
37 
38  private Connection conn = null;
39  private Statement stmt = null;
40 
41  Random r = new Random();
42 
43  int threadId;
45 
46  Worker(int ti, int nt) {
47  threadId = ti;
48  numOfTables = nt;
49 
50  try {
51  // STEP 2: Register JDBC driver
52  Class.forName(JDBC_DRIVER);
53 
54  // STEP 3: Open a connection
55  MAPDLOGGER.info(threadId + ": Connecting to database...");
56  conn = DriverManager.getConnection(DB_URL, USER, PASS);
57  stmt = conn.createStatement();
58 
59  for (int i = 0; i < numOfTables; i++) {
60  // doQuery(conn, "drop table t" + i, stmt);
61  doQuery(conn, "create table if not exists t" + i + " (newn int)", stmt);
62  }
63  } catch (SQLException se) {
64  // Handle errors for JDBC
65  se.printStackTrace();
66 
67  } catch (ClassNotFoundException ex) {
68  ex.printStackTrace();
69  } // end try
70  }
71 
72  public void run() {
73  try {
74  int tab = this.threadId;
75  for (int i = 0; i < 100; i++) {
76  doInsertQuery(conn, stmt, tab);
77  doSelectQuery(conn, stmt, tab);
78  tab++;
79  if (tab >= this.numOfTables) {
80  tab = 0;
81  }
82  }
83 
84  conn.close();
85  } catch (SQLException se) {
86  // Handle errors for JDBC
87  se.printStackTrace();
88  } catch (Exception e) {
89  // Handle errors for Class.forName
90  e.printStackTrace();
91  } finally {
92  // finally block used to close resources
93  try {
94  if (stmt != null) {
95  stmt.close();
96  }
97  } catch (SQLException se2) {
98  } // nothing we can do
99  try {
100  if (conn != null) {
101  conn.close();
102  }
103  } catch (SQLException se) {
104  se.printStackTrace();
105  } // end finally try
106  } // end try
107  } // end main
108 
109  private void doQuery(Connection conn, String sql, Statement stmt) throws SQLException {
110  ResultSet rs = null;
111  try {
112  rs = stmt.executeQuery(sql);
113  } catch (SQLException se) {
114  if (se.getMessage().contains("does not exist")) {
115  MAPDLOGGER.info(threadId + ": table not present");
116  return;
117  } else {
118  throw se;
119  }
120  }
121  while (rs.next()) {
122  int sum = rs.getInt(1);
123 
124  MAPDLOGGER.info(threadId + ": result " + sum);
125  }
126  rs.close();
127  }
128 
129  private void doInsertQuery(Connection conn, Statement stmt, int tab)
130  throws SQLException {
131  doQuery(conn, "insert into t" + tab + " values (1)", stmt);
132  }
133 
134  private void doSelectQuery(Connection conn, Statement stmt, int tab)
135  throws SQLException {
136  doQuery(conn, ("select sum(newn) from t" + tab), stmt);
137  }
138 
139 } // end FirstExample
void doQuery(Connection conn, String sql, Statement stmt)
Definition: Worker.java:109
Random r
Definition: Worker.java:41
auto sql(const std::string &sql_stmts)
Worker(int ti, int nt)
Definition: Worker.java:46
int threadId
Definition: Worker.java:43
static final Logger MAPDLOGGER
Definition: Worker.java:29
static final String JDBC_DRIVER
Definition: Worker.java:31
void run()
Definition: Worker.java:72
int numOfTables
Definition: Worker.java:44
Statement stmt
Definition: Worker.java:39
Connection conn
Definition: Worker.java:38
void doSelectQuery(Connection conn, Statement stmt, int tab)
Definition: Worker.java:134
void doInsertQuery(Connection conn, Statement stmt, int tab)
Definition: Worker.java:129
static final String PASS
Definition: Worker.java:36
static final String USER
Definition: Worker.java:35
static final String DB_URL
Definition: Worker.java:32