Advertisement
Guest User

Untitled

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