Advertisement
Guest User

Untitled

a guest
Jun 12th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.19 KB | None | 0 0
  1. package pl.kyku;
  2.  
  3. import java.sql.Array;
  4. import java.sql.Blob;
  5. import java.sql.CallableStatement;
  6. import java.sql.Clob;
  7. import java.sql.Connection;
  8. import java.sql.DatabaseMetaData;
  9. import java.sql.DriverManager;
  10. import java.sql.NClob;
  11. import java.sql.PreparedStatement;
  12. import java.sql.SQLClientInfoException;
  13. import java.sql.SQLException;
  14. import java.sql.SQLWarning;
  15. import java.sql.SQLXML;
  16. import java.sql.Savepoint;
  17. import java.sql.Statement;
  18. import java.sql.Struct;
  19. import java.util.Enumeration;
  20. import java.util.Map;
  21. import java.util.Properties;
  22. import java.util.Vector;
  23.  
  24. public class ConnectionPool
  25. {
  26. private final Vector<JDCConnection> connections;
  27. private final String url;
  28. private final String user;
  29. private final String password;
  30. private final long timeout = 60000L;
  31. private final ConnectionReaper reaper;
  32. private final int poolsize = 10;
  33.  
  34. public ConnectionPool(String url, String user, String password)
  35. throws ClassNotFoundException
  36. {
  37. Class.forName("com.mysql.jdbc.Driver");
  38. this.url = url;
  39. this.user = user;
  40. this.password = password;
  41. this.connections = new Vector(10);
  42. this.reaper = new ConnectionReaper(null);
  43. this.reaper.start();
  44. }
  45.  
  46. public synchronized Connection getConnection()
  47. throws SQLException
  48. {
  49. for (int i = 0; i < this.connections.size(); i++)
  50. {
  51. JDCConnection conn = (JDCConnection)this.connections.get(i);
  52. if (conn.lease())
  53. {
  54. if (conn.isValid()) {
  55. return conn;
  56. }
  57. this.connections.remove(conn);
  58. conn.terminate();
  59. }
  60. }
  61. JDCConnection conn = new JDCConnection(DriverManager.getConnection(this.url, this.user, this.password));
  62. conn.lease();
  63. if (!conn.isValid())
  64. {
  65. conn.terminate();
  66. throw new SQLException("Failed to validate a brand new connection");
  67. }
  68. this.connections.add(conn);
  69. return conn;
  70. }
  71.  
  72. private synchronized void reapConnections()
  73. {
  74. long stale = System.currentTimeMillis() - 60000L;
  75. for (JDCConnection conn : this.connections) {
  76. if ((conn.inUse()) && (stale > conn.getLastUse()) && (!conn.isValid())) {
  77. this.connections.remove(conn);
  78. }
  79. }
  80. }
  81.  
  82. public synchronized void closeConnections()
  83. {
  84. Enumeration<JDCConnection> conns = this.connections.elements();
  85. while (conns.hasMoreElements())
  86. {
  87. JDCConnection conn = (JDCConnection)conns.nextElement();
  88. this.connections.remove(conn);
  89. conn.terminate();
  90. }
  91. }
  92.  
  93. private class ConnectionReaper
  94. extends Thread
  95. {
  96. private ConnectionReaper() {}
  97.  
  98. public void run()
  99. {
  100. for (;;)
  101. {
  102. try
  103. {
  104. Thread.sleep(300000L);
  105. }
  106. catch (InterruptedException e) {}
  107. ConnectionPool.this.reapConnections();
  108. }
  109. }
  110. }
  111.  
  112. private class JDCConnection
  113. implements Connection
  114. {
  115. private final Connection conn;
  116. private boolean inuse;
  117. private long timestamp;
  118.  
  119. public JDCConnection(Connection conn)
  120. {
  121. this.conn = conn;
  122. this.inuse = false;
  123. this.timestamp = 0L;
  124. }
  125.  
  126. public void terminate()
  127. {
  128. try
  129. {
  130. this.conn.close();
  131. }
  132. catch (SQLException ex) {}
  133. }
  134.  
  135. public synchronized boolean lease()
  136. {
  137. if (this.inuse) {
  138. return false;
  139. }
  140. this.inuse = true;
  141. this.timestamp = System.currentTimeMillis();
  142. return true;
  143. }
  144.  
  145. public boolean inUse()
  146. {
  147. return this.inuse;
  148. }
  149.  
  150. public long getLastUse()
  151. {
  152. return this.timestamp;
  153. }
  154.  
  155. public void close()
  156. {
  157. this.inuse = false;
  158. try
  159. {
  160. if (!this.conn.getAutoCommit()) {
  161. this.conn.setAutoCommit(true);
  162. }
  163. }
  164. catch (SQLException ex)
  165. {
  166. ConnectionPool.this.connections.remove(this.conn);
  167. terminate();
  168. }
  169. }
  170.  
  171. public PreparedStatement prepareStatement(String sql)
  172. throws SQLException
  173. {
  174. return this.conn.prepareStatement(sql);
  175. }
  176.  
  177. public CallableStatement prepareCall(String sql)
  178. throws SQLException
  179. {
  180. return this.conn.prepareCall(sql);
  181. }
  182.  
  183. public Statement createStatement()
  184. throws SQLException
  185. {
  186. return this.conn.createStatement();
  187. }
  188.  
  189. public String nativeSQL(String sql)
  190. throws SQLException
  191. {
  192. return this.conn.nativeSQL(sql);
  193. }
  194.  
  195. public void setAutoCommit(boolean autoCommit)
  196. throws SQLException
  197. {
  198. this.conn.setAutoCommit(autoCommit);
  199. }
  200.  
  201. public boolean getAutoCommit()
  202. throws SQLException
  203. {
  204. return this.conn.getAutoCommit();
  205. }
  206.  
  207. public void commit()
  208. throws SQLException
  209. {
  210. this.conn.commit();
  211. }
  212.  
  213. public void rollback()
  214. throws SQLException
  215. {
  216. this.conn.rollback();
  217. }
  218.  
  219. public boolean isClosed()
  220. throws SQLException
  221. {
  222. return this.conn.isClosed();
  223. }
  224.  
  225. public DatabaseMetaData getMetaData()
  226. throws SQLException
  227. {
  228. return this.conn.getMetaData();
  229. }
  230.  
  231. public void setReadOnly(boolean readOnly)
  232. throws SQLException
  233. {
  234. this.conn.setReadOnly(readOnly);
  235. }
  236.  
  237. public boolean isReadOnly()
  238. throws SQLException
  239. {
  240. return this.conn.isReadOnly();
  241. }
  242.  
  243. public void setCatalog(String catalog)
  244. throws SQLException
  245. {
  246. this.conn.setCatalog(catalog);
  247. }
  248.  
  249. public String getCatalog()
  250. throws SQLException
  251. {
  252. return this.conn.getCatalog();
  253. }
  254.  
  255. public void setTransactionIsolation(int level)
  256. throws SQLException
  257. {
  258. this.conn.setTransactionIsolation(level);
  259. }
  260.  
  261. public int getTransactionIsolation()
  262. throws SQLException
  263. {
  264. return this.conn.getTransactionIsolation();
  265. }
  266.  
  267. public SQLWarning getWarnings()
  268. throws SQLException
  269. {
  270. return this.conn.getWarnings();
  271. }
  272.  
  273. public void clearWarnings()
  274. throws SQLException
  275. {
  276. this.conn.clearWarnings();
  277. }
  278.  
  279. public Array createArrayOf(String typeName, Object[] elements)
  280. throws SQLException
  281. {
  282. return this.conn.createArrayOf(typeName, elements);
  283. }
  284.  
  285. public Blob createBlob()
  286. throws SQLException
  287. {
  288. return this.conn.createBlob();
  289. }
  290.  
  291. public Clob createClob()
  292. throws SQLException
  293. {
  294. return this.conn.createClob();
  295. }
  296.  
  297. public NClob createNClob()
  298. throws SQLException
  299. {
  300. return this.conn.createNClob();
  301. }
  302.  
  303. public SQLXML createSQLXML()
  304. throws SQLException
  305. {
  306. return this.conn.createSQLXML();
  307. }
  308.  
  309. public Statement createStatement(int resultSetType, int resultSetConcurrency)
  310. throws SQLException
  311. {
  312. return this.conn.createStatement(resultSetType, resultSetConcurrency);
  313. }
  314.  
  315. public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability)
  316. throws SQLException
  317. {
  318. return this.conn.createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
  319. }
  320.  
  321. public Struct createStruct(String typeName, Object[] attributes)
  322. throws SQLException
  323. {
  324. return this.conn.createStruct(typeName, attributes);
  325. }
  326.  
  327. public Properties getClientInfo()
  328. throws SQLException
  329. {
  330. return this.conn.getClientInfo();
  331. }
  332.  
  333. public String getClientInfo(String name)
  334. throws SQLException
  335. {
  336. return this.conn.getClientInfo(name);
  337. }
  338.  
  339. public int getHoldability()
  340. throws SQLException
  341. {
  342. return this.conn.getHoldability();
  343. }
  344.  
  345. public Map<String, Class<?>> getTypeMap()
  346. throws SQLException
  347. {
  348. return this.conn.getTypeMap();
  349. }
  350.  
  351. public boolean isValid()
  352. {
  353. try
  354. {
  355. return this.conn.isValid(1);
  356. }
  357. catch (SQLException ex) {}
  358. return false;
  359. }
  360.  
  361. public boolean isValid(int timeout)
  362. throws SQLException
  363. {
  364. return this.conn.isValid(timeout);
  365. }
  366.  
  367. public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency)
  368. throws SQLException
  369. {
  370. return this.conn.prepareCall(sql, resultSetType, resultSetConcurrency);
  371. }
  372.  
  373. public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
  374. throws SQLException
  375. {
  376. return this.conn.prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
  377. }
  378.  
  379. public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
  380. throws SQLException
  381. {
  382. return this.conn.prepareStatement(sql, autoGeneratedKeys);
  383. }
  384.  
  385. public PreparedStatement prepareStatement(String sql, int[] columnIndexes)
  386. throws SQLException
  387. {
  388. return this.conn.prepareStatement(sql, columnIndexes);
  389. }
  390.  
  391. public PreparedStatement prepareStatement(String sql, String[] columnNames)
  392. throws SQLException
  393. {
  394. return this.conn.prepareStatement(sql, columnNames);
  395. }
  396.  
  397. public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
  398. throws SQLException
  399. {
  400. return this.conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
  401. }
  402.  
  403. public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability)
  404. throws SQLException
  405. {
  406. return this.conn.prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
  407. }
  408.  
  409. public void releaseSavepoint(Savepoint savepoint)
  410. throws SQLException
  411. {
  412. this.conn.releaseSavepoint(savepoint);
  413. }
  414.  
  415. public void rollback(Savepoint savepoint)
  416. throws SQLException
  417. {
  418. this.conn.rollback(savepoint);
  419. }
  420.  
  421. public void setClientInfo(Properties properties)
  422. throws SQLClientInfoException
  423. {
  424. this.conn.setClientInfo(properties);
  425. }
  426.  
  427. public void setClientInfo(String name, String value)
  428. throws SQLClientInfoException
  429. {
  430. this.conn.setClientInfo(name, value);
  431. }
  432.  
  433. public void setHoldability(int holdability)
  434. throws SQLException
  435. {
  436. this.conn.setHoldability(holdability);
  437. }
  438.  
  439. public Savepoint setSavepoint()
  440. throws SQLException
  441. {
  442. return this.conn.setSavepoint();
  443. }
  444.  
  445. public Savepoint setSavepoint(String name)
  446. throws SQLException
  447. {
  448. return this.conn.setSavepoint(name);
  449. }
  450.  
  451. public void setTypeMap(Map<String, Class<?>> map)
  452. throws SQLException
  453. {
  454. this.conn.setTypeMap(map);
  455. }
  456.  
  457. public boolean isWrapperFor(Class<?> iface)
  458. throws SQLException
  459. {
  460. return this.conn.isWrapperFor(iface);
  461. }
  462.  
  463. public <T> T unwrap(Class<T> iface)
  464. throws SQLException
  465. {
  466. return (T)this.conn.unwrap(iface);
  467. }
  468. }
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement