1 /* $Id: PooledConnection.java 94 2008-12-18 11:07:17Z dobashi $
\r
3 * (c)2005 Lavans Networks Inc. All Rights Reserved.
\r
5 package com.lavans.luz2.sql.pool;
\r
7 import java.sql.Array;
\r
8 import java.sql.Blob;
\r
9 import java.sql.CallableStatement;
\r
10 import java.sql.Clob;
\r
11 import java.sql.DatabaseMetaData;
\r
12 import java.sql.NClob;
\r
13 import java.sql.PreparedStatement;
\r
14 import java.sql.SQLClientInfoException;
\r
15 import java.sql.SQLException;
\r
16 import java.sql.SQLWarning;
\r
17 import java.sql.SQLXML;
\r
18 import java.sql.Savepoint;
\r
19 import java.sql.Statement;
\r
20 import java.sql.Struct;
\r
21 import java.util.ArrayList;
\r
22 import java.util.Collections;
\r
23 import java.util.List;
\r
24 import java.util.Map;
\r
25 import java.util.Properties;
\r
27 import org.apache.commons.logging.Log;
\r
28 import org.apache.commons.logging.LogFactory;
\r
30 import com.lavans.luz2.sql.ConnectionPool;
\r
31 import com.lavans.luz2.sql.DBManager;
\r
32 import com.lavans.luz2.sql.bind.BindCallableStatement;
\r
33 import com.lavans.luz2.sql.bind.BindConnection;
\r
34 import com.lavans.luz2.sql.bind.BindPreparedStatement;
\r
38 * DBManager.releaseConnection(con)を呼ばなくても、con.close()すると
\r
39 * コネクションを返却するように振る舞うためのラッパークラス。
\r
41 * 各種Statementを作成するときは、PooledStatementでラップする。
\r
42 * PooledStatement#close()では本クラスのremoveStatement()を呼び出すので、
\r
43 * Connection#close()が呼ばれずに何度もcreateStatement()/st.close()を
\r
44 * 繰り返すような場合に本クラスのstatementList()が肥大していくのを防ぐ。
\r
45 * DriverWrapper経由で呼び出された場合など、lavansutilの外側で
\r
46 * さらにConnectionPoolするような場合に必要である。
\r
50 public class PooledConnection implements BindConnection {
\r
52 private static Log logger = LogFactory.getLog(PooledConnection.class);
\r
54 private BindConnection con=null;
\r
55 private ConnectionPool pool = null;
\r
57 /** close()時にすべてのStatementを自動的に閉じる。 */
\r
58 private List<PooledStatement> statementList = Collections.synchronizedList(new ArrayList<PooledStatement>());
\r
61 public BindConnection getRealConnection(){
\r
68 public PooledConnection(ConnectionPool pool, BindConnection bcon) {
\r
74 * 子Statementで明示的にclose()をかけた時に、こちらで
\r
79 public boolean removeStatement(PooledStatement st){
\r
80 logger.debug("before:"+ statementList.size());
\r
81 boolean result = statementList.remove(st);
\r
82 logger.debug("after:"+ statementList.size());
\r
88 * BindConnectionImpleに処理委譲。
\r
89 * @see com.lavans.util.jdbc.bind.BindConnectionImpl#bindPrepareStatement(java.lang.String)
\r
91 public BindPreparedStatement bindPrepareStatement(String sql)
\r
92 throws SQLException {
\r
93 BindPreparedStatement bst = con.bindPrepareStatement(sql);
\r
94 PooledBindPreparedStatement pst = new PooledBindPreparedStatement(this,bst);
\r
95 statementList.add(pst);
\r
100 * BindConnectionImpleに処理委譲。
\r
101 * @see com.lavans.util.jdbc.bind.BindConnectionImpl#bindPrepareStatement(java.lang.String)
\r
103 public BindCallableStatement bindPrepareCall(String sql)
\r
104 throws SQLException {
\r
105 BindCallableStatement bst = con.bindPrepareCall(sql);
\r
106 PooledBindCallableStatement pst = new PooledBindCallableStatement(this,bst);
\r
107 statementList.add(pst);
\r
112 * DBManagerを通じてコネクションプールに返却。
\r
114 public void close() throws SQLException{
\r
115 DBManager.releaseConnection(this,pool);
\r
116 // DBManager#releaseConnection()->ConnectionPool#releaseConnection()の中で
\r
117 // clearStatementList()を呼び出すのでここで呼ぶ必要はない。
\r
118 // ConnectionPoolクラスでpoolListに移し終わっているので、呼んではいけない。
\r
122 * 保存しておいたstatementのクリア。
\r
125 public void clearStatementList(){
\r
126 // close()をかけると本クラスのremoveStatementを呼ぶ。
\r
127 while(statementList.size()>0){
\r
128 logger.warn("statementList.size()>0 st.close()の呼び忘れが考えられる", new Exception());
\r
129 Statement st = statementList.remove(0);
\r
130 if(!(st instanceof PooledStatement)){
\r
131 logger.error("PooledでないStatementを保持");
\r
133 PooledStatement pst = (PooledStatement)st;
\r
134 logger.warn("this:"+this.toString() +" st.parent:"+pst.parent.toString());
\r
138 } catch (Exception e) {
\r
141 statementList.clear();
\r
145 * @throws java.sql.SQLException
\r
147 public void clearWarnings() throws SQLException {
\r
148 con.clearWarnings();
\r
151 * @throws java.sql.SQLException
\r
153 public void commit() throws SQLException {
\r
158 * @throws java.sql.SQLException
\r
160 public Statement createStatement() throws SQLException {
\r
161 Statement st = con.createStatement();
\r
162 PooledStatement pst = new PooledStatement(this,st);
\r
163 statementList.add(pst);
\r
170 * @throws java.sql.SQLException
\r
172 public Statement createStatement(int arg0, int arg1) throws SQLException {
\r
173 Statement st = con.createStatement(arg0, arg1);
\r
174 PooledStatement pst = new PooledStatement(this,st);
\r
175 statementList.add(pst);
\r
183 * @throws java.sql.SQLException
\r
185 public Statement createStatement(int arg0, int arg1, int arg2)
\r
186 throws SQLException {
\r
187 Statement st = con.createStatement(arg0, arg1, arg2);
\r
188 PooledStatement pst = new PooledStatement(this,st);
\r
189 statementList.add(pst);
\r
194 * @throws java.sql.SQLException
\r
196 public boolean getAutoCommit() throws SQLException {
\r
197 return con.getAutoCommit();
\r
201 * @throws java.sql.SQLException
\r
203 public String getCatalog() throws SQLException {
\r
204 return con.getCatalog();
\r
208 * @throws java.sql.SQLException
\r
210 public int getHoldability() throws SQLException {
\r
211 return con.getHoldability();
\r
215 * @throws java.sql.SQLException
\r
217 public DatabaseMetaData getMetaData() throws SQLException {
\r
218 return con.getMetaData();
\r
222 * @throws java.sql.SQLException
\r
224 public int getTransactionIsolation() throws SQLException {
\r
225 return con.getTransactionIsolation();
\r
229 * @throws java.sql.SQLException
\r
231 public Map<String, Class<?>> getTypeMap() throws SQLException {
\r
232 return con.getTypeMap();
\r
236 * @throws java.sql.SQLException
\r
238 public SQLWarning getWarnings() throws SQLException {
\r
239 return con.getWarnings();
\r
244 * @throws java.sql.SQLException
\r
246 public boolean isClosed() throws SQLException {
\r
247 return con.isClosed();
\r
251 * @throws java.sql.SQLException
\r
253 public boolean isReadOnly() throws SQLException {
\r
254 return con.isReadOnly();
\r
259 * @throws java.sql.SQLException
\r
261 public String nativeSQL(String arg0) throws SQLException {
\r
262 return con.nativeSQL(arg0);
\r
267 * @throws java.sql.SQLException
\r
269 public CallableStatement prepareCall(String arg0) throws SQLException {
\r
270 CallableStatement st = con.prepareCall(arg0);
\r
271 PooledCallableStatement pst = new PooledCallableStatement(this,st);
\r
272 statementList.add(pst);
\r
280 * @throws java.sql.SQLException
\r
282 public CallableStatement prepareCall(String arg0, int arg1, int arg2)
\r
283 throws SQLException {
\r
284 CallableStatement st = con.prepareCall(arg0, arg1, arg2);
\r
285 PooledCallableStatement pst = new PooledCallableStatement(this,st);
\r
286 statementList.add(pst);
\r
295 * @throws java.sql.SQLException
\r
297 public CallableStatement prepareCall(String arg0, int arg1, int arg2,
\r
298 int arg3) throws SQLException {
\r
299 CallableStatement st = con.prepareCall(arg0, arg1, arg2, arg3);
\r
300 PooledCallableStatement pst = new PooledCallableStatement(this,st);
\r
301 statementList.add(pst);
\r
307 * @throws java.sql.SQLException
\r
309 public PreparedStatement prepareStatement(String arg0) throws SQLException {
\r
310 PreparedStatement st = con.prepareStatement(arg0);
\r
311 PooledPreparedStatement pst = new PooledPreparedStatement(this,st);
\r
312 statementList.add(pst);
\r
319 * @throws java.sql.SQLException
\r
321 public PreparedStatement prepareStatement(String arg0, int arg1)
\r
322 throws SQLException {
\r
323 PreparedStatement st = con.prepareStatement(arg0, arg1);
\r
324 PooledPreparedStatement pst = new PooledPreparedStatement(this,st);
\r
325 statementList.add(pst);
\r
334 * @throws java.sql.SQLException
\r
336 public PreparedStatement prepareStatement(String arg0, int arg1, int arg2)
\r
337 throws SQLException {
\r
338 PreparedStatement st = con.prepareStatement(arg0, arg1, arg2);
\r
339 PooledPreparedStatement pst = new PooledPreparedStatement(this,st);
\r
340 statementList.add(pst);
\r
350 * @throws java.sql.SQLException
\r
352 public PreparedStatement prepareStatement(String arg0, int arg1, int arg2,
\r
353 int arg3) throws SQLException {
\r
354 PreparedStatement st = con.prepareStatement(arg0, arg1, arg2, arg3);
\r
355 PooledPreparedStatement pst = new PooledPreparedStatement(this,st);
\r
356 statementList.add(pst);
\r
364 * @throws java.sql.SQLException
\r
366 public PreparedStatement prepareStatement(String arg0, int[] arg1)
\r
367 throws SQLException {
\r
368 PreparedStatement st = con.prepareStatement(arg0, arg1);
\r
369 PooledPreparedStatement pst = new PooledPreparedStatement(this,st);
\r
370 statementList.add(pst);
\r
378 * @throws java.sql.SQLException
\r
380 public PreparedStatement prepareStatement(String arg0, String[] arg1)
\r
381 throws SQLException {
\r
382 PreparedStatement st = con.prepareStatement(arg0, arg1);
\r
383 PooledPreparedStatement pst = new PooledPreparedStatement(this,st);
\r
384 statementList.add(pst);
\r
390 * @throws java.sql.SQLException
\r
392 public void releaseSavepoint(Savepoint arg0) throws SQLException {
\r
393 con.releaseSavepoint(arg0);
\r
396 * @throws java.sql.SQLException
\r
398 public void rollback() throws SQLException {
\r
403 * @throws java.sql.SQLException
\r
405 public void rollback(Savepoint arg0) throws SQLException {
\r
406 con.rollback(arg0);
\r
410 * @throws java.sql.SQLException
\r
412 public void setAutoCommit(boolean arg0) throws SQLException {
\r
413 con.setAutoCommit(arg0);
\r
417 * @throws java.sql.SQLException
\r
419 public void setCatalog(String arg0) throws SQLException {
\r
420 con.setCatalog(arg0);
\r
424 * @throws java.sql.SQLException
\r
426 public void setHoldability(int arg0) throws SQLException {
\r
427 con.setHoldability(arg0);
\r
431 * @throws java.sql.SQLException
\r
433 public void setReadOnly(boolean arg0) throws SQLException {
\r
434 con.setReadOnly(arg0);
\r
438 * @throws java.sql.SQLException
\r
440 public Savepoint setSavepoint() throws SQLException {
\r
441 return con.setSavepoint();
\r
446 * @throws java.sql.SQLException
\r
448 public Savepoint setSavepoint(String arg0) throws SQLException {
\r
449 return con.setSavepoint(arg0);
\r
453 * @throws java.sql.SQLException
\r
455 public void setTransactionIsolation(int arg0) throws SQLException {
\r
456 con.setTransactionIsolation(arg0);
\r
460 * @throws java.sql.SQLException
\r
462 public void setTypeMap(Map<String, Class<?>> arg0) throws SQLException {
\r
463 con.setTypeMap(arg0);
\r
469 * @throws SQLException
\r
470 * @see java.sql.Connection#createArrayOf(java.lang.String, java.lang.Object[])
\r
472 public Array createArrayOf(String typeName, Object[] elements)
\r
473 throws SQLException {
\r
474 return con.createArrayOf(typeName, elements);
\r
479 * @throws SQLException
\r
480 * @see java.sql.Connection#createBlob()
\r
482 public Blob createBlob() throws SQLException {
\r
483 return con.createBlob();
\r
488 * @throws SQLException
\r
489 * @see java.sql.Connection#createClob()
\r
491 public Clob createClob() throws SQLException {
\r
492 return con.createClob();
\r
497 * @throws SQLException
\r
498 * @see java.sql.Connection#createNClob()
\r
500 public NClob createNClob() throws SQLException {
\r
501 return con.createNClob();
\r
506 * @throws SQLException
\r
507 * @see java.sql.Connection#createSQLXML()
\r
509 public SQLXML createSQLXML() throws SQLException {
\r
510 return con.createSQLXML();
\r
515 * @param attributes
\r
517 * @throws SQLException
\r
518 * @see java.sql.Connection#createStruct(java.lang.String, java.lang.Object[])
\r
520 public Struct createStruct(String typeName, Object[] attributes)
\r
521 throws SQLException {
\r
522 return con.createStruct(typeName, attributes);
\r
527 * @throws SQLException
\r
528 * @see java.sql.Connection#getClientInfo()
\r
530 public Properties getClientInfo() throws SQLException {
\r
531 return con.getClientInfo();
\r
537 * @throws SQLException
\r
538 * @see java.sql.Connection#getClientInfo(java.lang.String)
\r
540 public String getClientInfo(String name) throws SQLException {
\r
541 return con.getClientInfo(name);
\r
547 * @throws SQLException
\r
548 * @see java.sql.Connection#isValid(int)
\r
550 public boolean isValid(int timeout) throws SQLException {
\r
551 return con.isValid(timeout);
\r
557 * @throws SQLException
\r
558 * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
\r
560 public boolean isWrapperFor(Class<?> iface) throws SQLException {
\r
561 return con.isWrapperFor(iface);
\r
565 * @param properties
\r
566 * @throws SQLClientInfoException
\r
567 * @see java.sql.Connection#setClientInfo(java.util.Properties)
\r
569 public void setClientInfo(Properties properties)
\r
570 throws SQLClientInfoException {
\r
571 con.setClientInfo(properties);
\r
577 * @throws SQLClientInfoException
\r
578 * @see java.sql.Connection#setClientInfo(java.lang.String, java.lang.String)
\r
580 public void setClientInfo(String name, String value)
\r
581 throws SQLClientInfoException {
\r
582 con.setClientInfo(name, value);
\r
589 * @throws SQLException
\r
590 * @see java.sql.Wrapper#unwrap(java.lang.Class)
\r
592 @SuppressWarnings("unchecked")
\r
593 public <T> T unwrap(Class<T> iface) throws SQLException {
\r