OSDN Git Service

import
[luz/luz.git] / luz2 / src / com / lavans / luz2 / sql / pool / PooledConnection.java
1 /* $Id: PooledConnection.java 94 2008-12-18 11:07:17Z dobashi $\r
2  * create: 2005/01/27\r
3  * (c)2005 Lavans Networks Inc. All Rights Reserved.\r
4  */\r
5 package com.lavans.luz2.sql.pool;\r
6 \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
26 \r
27 import org.apache.commons.logging.Log;\r
28 import org.apache.commons.logging.LogFactory;\r
29 \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
35 \r
36 \r
37 /**\r
38  * DBManager.releaseConnection(con)を呼ばなくても、con.close()すると\r
39  * コネクションを返却するように振る舞うためのラッパークラス。\r
40  *\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
47  * @author dobashi\r
48  *\r
49  */\r
50 public class PooledConnection implements BindConnection {\r
51         /** ロガー。debug用 */\r
52         private static Log logger = LogFactory.getLog(PooledConnection.class);\r
53 \r
54         private BindConnection con=null;\r
55         private ConnectionPool pool = null;\r
56 \r
57         /** close()時にすべてのStatementを自動的に閉じる。 */\r
58         private List<PooledStatement> statementList = Collections.synchronizedList(new ArrayList<PooledStatement>());\r
59 \r
60 \r
61         public BindConnection getRealConnection(){\r
62                 return con;\r
63         }\r
64 \r
65         /**\r
66          *\r
67          */\r
68         public PooledConnection(ConnectionPool pool, BindConnection bcon) {\r
69                 this.pool = pool;\r
70                 this.con = bcon;\r
71         }\r
72 \r
73         /**\r
74          * 子Statementで明示的にclose()をかけた時に、こちらで\r
75          * 保存しておいたリストから削除する。\r
76          * @param st\r
77          * @return\r
78          */\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
83 \r
84                 return result;\r
85         }\r
86 \r
87         /**\r
88          * BindConnectionImpleに処理委譲。\r
89          * @see com.lavans.util.jdbc.bind.BindConnectionImpl#bindPrepareStatement(java.lang.String)\r
90          */\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
96                 return pst;\r
97         }\r
98 \r
99         /**\r
100          * BindConnectionImpleに処理委譲。\r
101          * @see com.lavans.util.jdbc.bind.BindConnectionImpl#bindPrepareStatement(java.lang.String)\r
102          */\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
108                 return pst;\r
109         }\r
110 \r
111         /**\r
112          * DBManagerを通じてコネクションプールに返却。\r
113          */\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
119         }\r
120 \r
121         /**\r
122          * 保存しておいたstatementのクリア。\r
123          *\r
124          */\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
132                         }else{\r
133                                 PooledStatement pst = (PooledStatement)st;\r
134                                 logger.warn("this:"+this.toString() +" st.parent:"+pst.parent.toString());\r
135                         }\r
136                         try {\r
137                                 st.close();\r
138                         } catch (Exception e) {\r
139                         }\r
140                 }\r
141                 statementList.clear();\r
142         }\r
143 \r
144         /**\r
145          * @throws java.sql.SQLException\r
146          */\r
147         public void clearWarnings() throws SQLException {\r
148                 con.clearWarnings();\r
149         }\r
150         /**\r
151          * @throws java.sql.SQLException\r
152          */\r
153         public void commit() throws SQLException {\r
154                 con.commit();\r
155         }\r
156         /**\r
157          * @return\r
158          * @throws java.sql.SQLException\r
159          */\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
164                 return pst;\r
165         }\r
166         /**\r
167          * @param arg0\r
168          * @param arg1\r
169          * @return\r
170          * @throws java.sql.SQLException\r
171          */\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
176                 return pst;\r
177         }\r
178         /**\r
179          * @param arg0\r
180          * @param arg1\r
181          * @param arg2\r
182          * @return\r
183          * @throws java.sql.SQLException\r
184          */\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
190                 return pst;\r
191         }\r
192         /**\r
193          * @return\r
194          * @throws java.sql.SQLException\r
195          */\r
196         public boolean getAutoCommit() throws SQLException {\r
197                 return con.getAutoCommit();\r
198         }\r
199         /**\r
200          * @return\r
201          * @throws java.sql.SQLException\r
202          */\r
203         public String getCatalog() throws SQLException {\r
204                 return con.getCatalog();\r
205         }\r
206         /**\r
207          * @return\r
208          * @throws java.sql.SQLException\r
209          */\r
210         public int getHoldability() throws SQLException {\r
211                 return con.getHoldability();\r
212         }\r
213         /**\r
214          * @return\r
215          * @throws java.sql.SQLException\r
216          */\r
217         public DatabaseMetaData getMetaData() throws SQLException {\r
218                 return con.getMetaData();\r
219         }\r
220         /**\r
221          * @return\r
222          * @throws java.sql.SQLException\r
223          */\r
224         public int getTransactionIsolation() throws SQLException {\r
225                 return con.getTransactionIsolation();\r
226         }\r
227         /**\r
228          * @return\r
229          * @throws java.sql.SQLException\r
230          */\r
231         public Map<String, Class<?>> getTypeMap() throws SQLException {\r
232                 return con.getTypeMap();\r
233         }\r
234         /**\r
235          * @return\r
236          * @throws java.sql.SQLException\r
237          */\r
238         public SQLWarning getWarnings() throws SQLException {\r
239                 return con.getWarnings();\r
240         }\r
241 \r
242         /**\r
243          * @return\r
244          * @throws java.sql.SQLException\r
245          */\r
246         public boolean isClosed() throws SQLException {\r
247                 return con.isClosed();\r
248         }\r
249         /**\r
250          * @return\r
251          * @throws java.sql.SQLException\r
252          */\r
253         public boolean isReadOnly() throws SQLException {\r
254                 return con.isReadOnly();\r
255         }\r
256         /**\r
257          * @param arg0\r
258          * @return\r
259          * @throws java.sql.SQLException\r
260          */\r
261         public String nativeSQL(String arg0) throws SQLException {\r
262                 return con.nativeSQL(arg0);\r
263         }\r
264         /**\r
265          * @param arg0\r
266          * @return\r
267          * @throws java.sql.SQLException\r
268          */\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
273                 return pst;\r
274         }\r
275         /**\r
276          * @param arg0\r
277          * @param arg1\r
278          * @param arg2\r
279          * @return\r
280          * @throws java.sql.SQLException\r
281          */\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
287                 return pst;\r
288         }\r
289         /**\r
290          * @param arg0\r
291          * @param arg1\r
292          * @param arg2\r
293          * @param arg3\r
294          * @return\r
295          * @throws java.sql.SQLException\r
296          */\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
302                 return pst;\r
303         }\r
304         /**\r
305          * @param arg0\r
306          * @return\r
307          * @throws java.sql.SQLException\r
308          */\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
313                 return pst;\r
314         }\r
315         /**\r
316          * @param arg0\r
317          * @param arg1\r
318          * @return\r
319          * @throws java.sql.SQLException\r
320          */\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
326                 return pst;\r
327 \r
328         }\r
329         /**\r
330          * @param arg0\r
331          * @param arg1\r
332          * @param arg2\r
333          * @return\r
334          * @throws java.sql.SQLException\r
335          */\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
341                 return pst;\r
342 \r
343         }\r
344         /**\r
345          * @param arg0\r
346          * @param arg1\r
347          * @param arg2\r
348          * @param arg3\r
349          * @return\r
350          * @throws java.sql.SQLException\r
351          */\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
357                 return pst;\r
358 \r
359         }\r
360         /**\r
361          * @param arg0\r
362          * @param arg1\r
363          * @return\r
364          * @throws java.sql.SQLException\r
365          */\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
371                 return pst;\r
372 \r
373         }\r
374         /**\r
375          * @param arg0\r
376          * @param arg1\r
377          * @return\r
378          * @throws java.sql.SQLException\r
379          */\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
385                 return pst;\r
386 \r
387         }\r
388         /**\r
389          * @param arg0\r
390          * @throws java.sql.SQLException\r
391          */\r
392         public void releaseSavepoint(Savepoint arg0) throws SQLException {\r
393                 con.releaseSavepoint(arg0);\r
394         }\r
395         /**\r
396          * @throws java.sql.SQLException\r
397          */\r
398         public void rollback() throws SQLException {\r
399                 con.rollback();\r
400         }\r
401         /**\r
402          * @param arg0\r
403          * @throws java.sql.SQLException\r
404          */\r
405         public void rollback(Savepoint arg0) throws SQLException {\r
406                 con.rollback(arg0);\r
407         }\r
408         /**\r
409          * @param arg0\r
410          * @throws java.sql.SQLException\r
411          */\r
412         public void setAutoCommit(boolean arg0) throws SQLException {\r
413                 con.setAutoCommit(arg0);\r
414         }\r
415         /**\r
416          * @param arg0\r
417          * @throws java.sql.SQLException\r
418          */\r
419         public void setCatalog(String arg0) throws SQLException {\r
420                 con.setCatalog(arg0);\r
421         }\r
422         /**\r
423          * @param arg0\r
424          * @throws java.sql.SQLException\r
425          */\r
426         public void setHoldability(int arg0) throws SQLException {\r
427                 con.setHoldability(arg0);\r
428         }\r
429         /**\r
430          * @param arg0\r
431          * @throws java.sql.SQLException\r
432          */\r
433         public void setReadOnly(boolean arg0) throws SQLException {\r
434                 con.setReadOnly(arg0);\r
435         }\r
436         /**\r
437          * @return\r
438          * @throws java.sql.SQLException\r
439          */\r
440         public Savepoint setSavepoint() throws SQLException {\r
441                 return con.setSavepoint();\r
442         }\r
443         /**\r
444          * @param arg0\r
445          * @return\r
446          * @throws java.sql.SQLException\r
447          */\r
448         public Savepoint setSavepoint(String arg0) throws SQLException {\r
449                 return con.setSavepoint(arg0);\r
450         }\r
451         /**\r
452          * @param arg0\r
453          * @throws java.sql.SQLException\r
454          */\r
455         public void setTransactionIsolation(int arg0) throws SQLException {\r
456                 con.setTransactionIsolation(arg0);\r
457         }\r
458         /**\r
459          * @param arg0\r
460          * @throws java.sql.SQLException\r
461          */\r
462         public void setTypeMap(Map<String, Class<?>> arg0) throws SQLException {\r
463                 con.setTypeMap(arg0);\r
464         }\r
465         /**\r
466          * @param typeName\r
467          * @param elements\r
468          * @return\r
469          * @throws SQLException\r
470          * @see java.sql.Connection#createArrayOf(java.lang.String, java.lang.Object[])\r
471          */\r
472         public Array createArrayOf(String typeName, Object[] elements)\r
473                         throws SQLException {\r
474                 return con.createArrayOf(typeName, elements);\r
475         }\r
476 \r
477         /**\r
478          * @return\r
479          * @throws SQLException\r
480          * @see java.sql.Connection#createBlob()\r
481          */\r
482         public Blob createBlob() throws SQLException {\r
483                 return con.createBlob();\r
484         }\r
485 \r
486         /**\r
487          * @return\r
488          * @throws SQLException\r
489          * @see java.sql.Connection#createClob()\r
490          */\r
491         public Clob createClob() throws SQLException {\r
492                 return con.createClob();\r
493         }\r
494 \r
495         /**\r
496          * @return\r
497          * @throws SQLException\r
498          * @see java.sql.Connection#createNClob()\r
499          */\r
500         public NClob createNClob() throws SQLException {\r
501                 return con.createNClob();\r
502         }\r
503 \r
504         /**\r
505          * @return\r
506          * @throws SQLException\r
507          * @see java.sql.Connection#createSQLXML()\r
508          */\r
509         public SQLXML createSQLXML() throws SQLException {\r
510                 return con.createSQLXML();\r
511         }\r
512 \r
513         /**\r
514          * @param typeName\r
515          * @param attributes\r
516          * @return\r
517          * @throws SQLException\r
518          * @see java.sql.Connection#createStruct(java.lang.String, java.lang.Object[])\r
519          */\r
520         public Struct createStruct(String typeName, Object[] attributes)\r
521                         throws SQLException {\r
522                 return con.createStruct(typeName, attributes);\r
523         }\r
524 \r
525         /**\r
526          * @return\r
527          * @throws SQLException\r
528          * @see java.sql.Connection#getClientInfo()\r
529          */\r
530         public Properties getClientInfo() throws SQLException {\r
531                 return con.getClientInfo();\r
532         }\r
533 \r
534         /**\r
535          * @param name\r
536          * @return\r
537          * @throws SQLException\r
538          * @see java.sql.Connection#getClientInfo(java.lang.String)\r
539          */\r
540         public String getClientInfo(String name) throws SQLException {\r
541                 return con.getClientInfo(name);\r
542         }\r
543 \r
544         /**\r
545          * @param timeout\r
546          * @return\r
547          * @throws SQLException\r
548          * @see java.sql.Connection#isValid(int)\r
549          */\r
550         public boolean isValid(int timeout) throws SQLException {\r
551                 return con.isValid(timeout);\r
552         }\r
553 \r
554         /**\r
555          * @param iface\r
556          * @return\r
557          * @throws SQLException\r
558          * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)\r
559          */\r
560         public boolean isWrapperFor(Class<?> iface) throws SQLException {\r
561                 return con.isWrapperFor(iface);\r
562         }\r
563 \r
564         /**\r
565          * @param properties\r
566          * @throws SQLClientInfoException\r
567          * @see java.sql.Connection#setClientInfo(java.util.Properties)\r
568          */\r
569         public void setClientInfo(Properties properties)\r
570                         throws SQLClientInfoException {\r
571                 con.setClientInfo(properties);\r
572         }\r
573 \r
574         /**\r
575          * @param name\r
576          * @param value\r
577          * @throws SQLClientInfoException\r
578          * @see java.sql.Connection#setClientInfo(java.lang.String, java.lang.String)\r
579          */\r
580         public void setClientInfo(String name, String value)\r
581                         throws SQLClientInfoException {\r
582                 con.setClientInfo(name, value);\r
583         }\r
584 \r
585         /**\r
586          * @param <T>\r
587          * @param iface\r
588          * @return\r
589          * @throws SQLException\r
590          * @see java.sql.Wrapper#unwrap(java.lang.Class)\r
591          */\r
592         @SuppressWarnings("unchecked")\r
593         public <T> T unwrap(Class<T> iface) throws SQLException {\r
594                 return (T)this;\r
595         }\r
596 }\r