PageRenderTime 27ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/projects/c_jdbc-2.0.2/test/src/org/objectweb/cjdbc/requestplayer/ClientThread.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 423 lines | 294 code | 35 blank | 94 comment | 78 complexity | d7659c8a4a622108316be40ea682e250 MD5 | raw file
  1. /**
  2. * C-JDBC: Clustered JDBC.
  3. * Copyright (C) 2002-2005 French National Institute For Research In Computer
  4. * Science And Control (INRIA).
  5. * Contact: c-jdbc@objectweb.org
  6. *
  7. * This library is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU Lesser General Public License as published by the
  9. * Free Software Foundation; either version 2.1 of the License, or any later
  10. * version.
  11. *
  12. * This library is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  15. * for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License
  18. * along with this library; if not, write to the Free Software Foundation,
  19. * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  20. *
  21. *
  22. * Initial developer(s): Emmanuel Cecchet.
  23. * Contributor(s): Julie Marguerite.
  24. */
  25. package org.objectweb.cjdbc.requestplayer;
  26. import java.sql.Connection;
  27. import java.sql.ResultSet;
  28. import java.sql.SQLException;
  29. import java.sql.Statement;
  30. import org.objectweb.cjdbc.common.util.Stats;
  31. /**
  32. * C-JDBC client emulator worker thread. Reads SQL requests in a file and
  33. * forwards them to the cache. If the cache returns no reply, this class
  34. * forwards the request to the database. Then it returns the reply and updates
  35. * the cache if needed.
  36. *
  37. * @author <a href="mailto:Emmanuel.Cecchet@inria.fr">Emmanuel Cecchet </a>
  38. * @author <a href="mailto:julie.marguerite@inria.fr">Julie Marguerite </a>
  39. * @version 1.0
  40. */
  41. public class ClientThread extends Thread
  42. {
  43. /** Debug on standard output. */
  44. private static final boolean DEBUG = false;
  45. /** Number of read requests. */
  46. private Stats selectStats = null;
  47. /** Number of unknown requests. */
  48. private Stats unknownStats = null;
  49. /** Number of update requests. */
  50. private Stats updateStats = null;
  51. /** Number of insert requests. */
  52. private Stats insertStats = null;
  53. /** Number of delete requests. */
  54. private Stats deleteStats = null;
  55. /** Number of transaction begin. */
  56. private Stats beginStats = null;
  57. /** Number of transaction commit. */
  58. private Stats commitStats = null;
  59. /** Number of transaction rollback. */
  60. private Stats rollbackStats = null;
  61. /** Statistics about get connection from driver */
  62. private Stats getConnectionStats = null;
  63. /** Statistics about closing a connection */
  64. private Stats closeStats = null;
  65. /** Statistics about getting request from the log file */
  66. private Stats getRequestStats = null;
  67. private Connection conn = null;
  68. private ClientEmulator father;
  69. private int threadId;
  70. /** Type of connection management: standard, fixed or pooling */
  71. private int connectionType;
  72. /**
  73. * Creates a new <code>ClientThread</code> instance.
  74. *
  75. * @param threadId thread id
  76. * @param father father client emulator
  77. * @param connectionType connection type
  78. */
  79. public ClientThread(int threadId, ClientEmulator father, int connectionType)
  80. {
  81. super("ClientThread" + threadId);
  82. // Init the pointers to the stats
  83. selectStats = father.getSelectStats();
  84. unknownStats = father.getUnknownStats();
  85. updateStats = father.getUpdateStats();
  86. insertStats = father.getInsertStats();
  87. deleteStats = father.getDeleteStats();
  88. beginStats = father.getBeginStats();
  89. commitStats = father.getCommitStats();
  90. rollbackStats = father.getRollbackStats();
  91. getRequestStats = father.getGetRequestStats();
  92. getConnectionStats = father.getGetConnectionStats();
  93. closeStats = father.getCloseStats();
  94. this.father = father;
  95. this.threadId = threadId;
  96. this.connectionType = connectionType;
  97. if (this.connectionType == RequestPlayerProperties.FIXED_CONNECTION)
  98. {
  99. // Get a new connection to the virtual database
  100. conn = father.getConnection();
  101. }
  102. }
  103. /**
  104. * @see java.lang.Runnable#run()
  105. */
  106. public void run()
  107. {
  108. String request = null;
  109. int tid = 0; // current transaction id
  110. if (DEBUG)
  111. System.out.println(threadId + ": Starting");
  112. // Get SQL requests from the trace file and send them to the DB
  113. while (true)
  114. {
  115. long startg = System.currentTimeMillis();
  116. request = father.parallelGetNextSQLRequest(tid);
  117. long endg = System.currentTimeMillis();
  118. getRequestStats.incrementCount();
  119. getRequestStats.updateTime(endg - startg);
  120. if (request == null)
  121. { // Could be the end of file or a transaction whose
  122. // commit/rollback was never logged in the trace file
  123. if (tid != 0)
  124. {
  125. System.out.println(threadId
  126. + ": Warning! Rollbacking unterminated transaction " + tid);
  127. request = "R";
  128. }
  129. else
  130. break; // No more requests
  131. }
  132. try
  133. {
  134. switch (request.charAt(0))
  135. {
  136. case 'B' :
  137. // Begin
  138. if (DEBUG)
  139. System.out.println(threadId + ": " + request);
  140. long startb = System.currentTimeMillis();
  141. if (connectionType != RequestPlayerProperties.FIXED_CONNECTION)
  142. {
  143. // Get a new connection to the virtual database
  144. conn = getConnection();
  145. }
  146. conn.setAutoCommit(false);
  147. long endb = System.currentTimeMillis();
  148. beginStats.incrementCount();
  149. beginStats.updateTime(endb - startb);
  150. tid = new Integer(request.substring(2)).intValue();
  151. break;
  152. case 'C' :
  153. // Commit
  154. if (DEBUG)
  155. System.out.println(threadId + ": " + request);
  156. long startc = System.currentTimeMillis();
  157. conn.commit();
  158. long endc = System.currentTimeMillis();
  159. commitStats.incrementCount();
  160. commitStats.updateTime(endc - startc);
  161. tid = 0;
  162. if (connectionType != RequestPlayerProperties.FIXED_CONNECTION)
  163. { // Close the connection
  164. closeConnection();
  165. }
  166. break;
  167. case 'R' :
  168. // Rollback
  169. if (DEBUG)
  170. System.out.println(threadId + ": " + request);
  171. long startr = System.currentTimeMillis();
  172. conn.rollback();
  173. long endr = System.currentTimeMillis();
  174. rollbackStats.incrementCount();
  175. rollbackStats.updateTime(endr - startr);
  176. tid = 0;
  177. if (connectionType != RequestPlayerProperties.FIXED_CONNECTION)
  178. { // Close the connection
  179. closeConnection();
  180. }
  181. break;
  182. case 'S' :
  183. // Select
  184. if (tid == 0
  185. && (connectionType != RequestPlayerProperties.FIXED_CONNECTION))
  186. {
  187. // Get a new connection to the virtual database
  188. conn = getConnection();
  189. // Execute the request
  190. execReadRequest(request);
  191. // Close the connection
  192. closeConnection();
  193. }
  194. else
  195. {
  196. execReadRequest(request);
  197. }
  198. break;
  199. case 'W' :
  200. // Write
  201. if (tid == 0
  202. && (connectionType != RequestPlayerProperties.FIXED_CONNECTION))
  203. {
  204. // Get a new connection to the virtual database
  205. conn = getConnection();
  206. // Execute the request
  207. execWriteRequest(request);
  208. // Close the connection
  209. closeConnection();
  210. }
  211. else
  212. {
  213. execWriteRequest(request);
  214. }
  215. break;
  216. default :
  217. System.err.println(threadId + ": Error! Unsupported request "
  218. + request);
  219. break;
  220. }
  221. }
  222. catch (Exception e)
  223. {
  224. System.err.println(threadId
  225. + ": An error occured while executing SQL request ("
  226. + e.getMessage() + ")");
  227. if (request.charAt(0) != 'S' && request.charAt(0) != 'W')
  228. { // Reset the tid for begin/commit/rollback
  229. if (tid != 0)
  230. {
  231. try
  232. {
  233. conn.rollback();
  234. }
  235. catch (Exception ignore)
  236. {
  237. }
  238. father.ignoreTid(tid);
  239. tid = 0;
  240. }
  241. }
  242. if (connectionType != RequestPlayerProperties.FIXED_CONNECTION)
  243. // Close the connection
  244. closeConnection();
  245. }
  246. }
  247. if (connectionType == RequestPlayerProperties.FIXED_CONNECTION)
  248. father.closeConnection(conn);
  249. // if (DEBUG)
  250. System.out.println(threadId + ": Ending.");
  251. }
  252. /**
  253. * Executes a write request.
  254. *
  255. * @param req request to execute
  256. */
  257. private void execWriteRequest(String req)
  258. {
  259. Statement stmt = null;
  260. String request = req.substring(2);
  261. if (DEBUG)
  262. System.out.println(threadId + ": " + request.substring(0, 5));
  263. long startw = System.currentTimeMillis();
  264. try
  265. {
  266. stmt = conn.createStatement();
  267. stmt.setQueryTimeout(father.getTimeout());
  268. stmt.executeUpdate(request);
  269. stmt.close();
  270. }
  271. catch (SQLException e)
  272. {
  273. if ((request.charAt(0) == 'i') || (request.charAt(0) == 'I')) // insert
  274. {
  275. insertStats.incrementError();
  276. }
  277. else if ((request.charAt(0) == 'u') || (request.charAt(0) == 'U')) // update
  278. {
  279. updateStats.incrementError();
  280. }
  281. else if ((request.charAt(0) == 'd') || (request.charAt(0) == 'D')) // delete
  282. {
  283. deleteStats.incrementError();
  284. }
  285. else
  286. {
  287. unknownStats.incrementError();
  288. }
  289. System.err.println(threadId + ": Failed to execute request: " + request
  290. + "(" + e + ")");
  291. return;
  292. }
  293. long endw = System.currentTimeMillis();
  294. if ((request.charAt(0) == 'i') || (request.charAt(0) == 'I')) // insert
  295. {
  296. insertStats.incrementCount();
  297. insertStats.updateTime(endw - startw);
  298. }
  299. else if ((request.charAt(0) == 'u') || (request.charAt(0) == 'U')) // update
  300. {
  301. updateStats.incrementCount();
  302. updateStats.updateTime(endw - startw);
  303. }
  304. else if ((request.charAt(0) == 'd') || (request.charAt(0) == 'D')) // delete
  305. {
  306. deleteStats.incrementCount();
  307. deleteStats.updateTime(endw - startw);
  308. }
  309. else
  310. {
  311. unknownStats.incrementCount();
  312. unknownStats.updateTime(endw - startw);
  313. }
  314. }
  315. /**
  316. * Executes a select request.
  317. *
  318. * @param req request to execute
  319. */
  320. private void execReadRequest(String req)
  321. {
  322. Statement stmt = null;
  323. ResultSet dbReply = null; // The reply from the database
  324. String request = req.substring(2);
  325. if (DEBUG)
  326. System.out.println(threadId + ": " + request.substring(0, 5));
  327. long startr = System.currentTimeMillis();
  328. try
  329. {
  330. stmt = conn.createStatement();
  331. stmt.setQueryTimeout(father.getTimeout());
  332. dbReply = stmt.executeQuery(request);
  333. // Parse the result if any
  334. if (dbReply != null)
  335. dbReply.next(); // Fetch only the first row
  336. stmt.close();
  337. }
  338. catch (SQLException e)
  339. {
  340. selectStats.incrementError();
  341. System.err.println(threadId + ": Failed to execute request: " + request
  342. + "(" + e + ")");
  343. }
  344. long endr = System.currentTimeMillis();
  345. selectStats.incrementCount();
  346. selectStats.updateTime(endr - startr);
  347. }
  348. /**
  349. * Closes the connection to the database.
  350. */
  351. private void closeConnection()
  352. {
  353. long start = System.currentTimeMillis();
  354. if (connectionType == RequestPlayerProperties.STANDARD_CONNECTION)
  355. {
  356. father.closeConnection(conn);
  357. }
  358. else if (connectionType == RequestPlayerProperties.POOLING_CONNECTION)
  359. {
  360. father.releaseConnectionToPool(conn);
  361. }
  362. long end = System.currentTimeMillis();
  363. closeStats.incrementCount();
  364. closeStats.updateTime(end - start);
  365. }
  366. /**
  367. * Gets a new connection to the database.
  368. *
  369. * @return Connection
  370. */
  371. private Connection getConnection()
  372. {
  373. Connection c = null;
  374. long start = System.currentTimeMillis();
  375. if (connectionType == RequestPlayerProperties.STANDARD_CONNECTION)
  376. {
  377. c = father.getConnection();
  378. }
  379. else if (connectionType == RequestPlayerProperties.POOLING_CONNECTION)
  380. {
  381. c = father.getConnectionFromPool();
  382. }
  383. long end = System.currentTimeMillis();
  384. getConnectionStats.incrementCount();
  385. getConnectionStats.updateTime(end - start);
  386. return c;
  387. }
  388. }