68
|
1 /*
|
|
2 Copyright (c) 2008 Franklin Schmidt <fschmidt@gmail.com>
|
|
3
|
|
4 Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5 of this software and associated documentation files (the "Software"), to deal
|
|
6 in the Software without restriction, including without limitation the rights
|
|
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8 copies of the Software, and to permit persons to whom the Software is
|
|
9 furnished to do so, subject to the following conditions:
|
|
10
|
|
11 The above copyright notice and this permission notice shall be included in
|
|
12 all copies or substantial portions of the Software.
|
|
13
|
|
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
20 THE SOFTWARE.
|
|
21 */
|
|
22
|
|
23 package fschmidt.db.pool;
|
|
24
|
|
25 import java.lang.reflect.InvocationHandler;
|
|
26 import java.lang.reflect.Method;
|
|
27 import java.sql.Connection;
|
|
28 import java.sql.SQLException;
|
|
29 import java.sql.Statement;
|
|
30 import java.util.List;
|
|
31 import java.util.ArrayList;
|
|
32 import java.util.Set;
|
|
33 import java.util.HashSet;
|
|
34 import org.slf4j.Logger;
|
|
35 import org.slf4j.LoggerFactory;
|
|
36 import fschmidt.db.DbDatabase;
|
|
37 import fschmidt.db.DbObject;
|
|
38 import fschmidt.db.DbKey;
|
|
39 import fschmidt.db.DbObjectFactory;
|
|
40 import fschmidt.db.SQLRuntimeException;
|
|
41 import fschmidt.db.extend.DbDatabaseExt;
|
|
42 import fschmidt.db.extend.DbTableExt;
|
|
43 import fschmidt.db.extend.DbTransaction;
|
|
44 import fschmidt.db.extend.DbRecordExt;
|
|
45 import fschmidt.db.extend.FilterDatabase;
|
|
46 import fschmidt.db.util.OverridingProxy;
|
|
47 import fschmidt.util.java.Stack;
|
|
48 import fschmidt.util.java.ArrayStack;
|
|
49
|
|
50
|
|
51 final class NestedConnection {
|
|
52
|
|
53 private static class MyOverridingProxy extends OverridingProxy<Connection,NestedConnection> {
|
|
54
|
|
55 MyOverridingProxy() {
|
|
56 super(Connection.class,NestedConnection.class);
|
|
57 }
|
|
58
|
|
59 protected InvocationHandler newInvocationHandler(Connection obj,NestedConnection override) {
|
|
60 return new MyOverridingInvocationHandler(obj,override);
|
|
61 }
|
|
62
|
|
63 protected class MyOverridingInvocationHandler extends OverridingInvocationHandler {
|
|
64
|
|
65 MyOverridingInvocationHandler(Connection obj,NestedConnection override) {
|
|
66 super(obj,override);
|
|
67 }
|
|
68
|
|
69 public Object invoke(Object proxy,Method method, Object[] args)
|
|
70 throws Throwable
|
|
71 {
|
|
72 if( !method.getName().equals("close") )
|
|
73 override.setUser();
|
|
74 return super.invoke(proxy,method,args);
|
|
75 }
|
|
76
|
|
77 }
|
|
78
|
|
79 }
|
|
80
|
|
81 private static final MyOverridingProxy factory = new MyOverridingProxy();
|
|
82
|
|
83 private final PooledConnection pooledCon;
|
|
84 final Connection proxyCon;
|
|
85 final Exception initException = new Exception("connection constructed in "+Thread.currentThread());
|
|
86 private boolean isClosed = false;
|
|
87 private final String user;
|
|
88
|
|
89 NestedConnection(PooledConnection pooledCon,String user) {
|
|
90 this.user = user;
|
|
91 this.pooledCon = pooledCon;
|
|
92 proxyCon = factory.newInstance(pooledCon.con(),this);
|
|
93 }
|
|
94
|
|
95 void setUser() throws SQLException {
|
|
96 pooledCon.setUser(user);
|
|
97 }
|
|
98
|
|
99 protected void checkClosed()
|
|
100 throws SQLException
|
|
101 {
|
|
102 if( isClosed )
|
|
103 throw new SQLException("connection is closed");
|
|
104 }
|
|
105
|
|
106 public void setAutoCommit(boolean autoCommit)
|
|
107 throws SQLException
|
|
108 {
|
|
109 checkClosed();
|
|
110 pooledCon.setAutoCommit(autoCommit);
|
|
111 }
|
|
112
|
|
113 public void commit()
|
|
114 throws SQLException
|
|
115 {
|
|
116 checkClosed();
|
|
117 pooledCon.commit();
|
|
118 }
|
|
119
|
|
120 public void rollback()
|
|
121 throws SQLException
|
|
122 {
|
|
123 checkClosed();
|
|
124 pooledCon.rollback();
|
|
125 }
|
|
126
|
|
127 public void close()
|
|
128 throws SQLException
|
|
129 {
|
|
130 checkClosed();
|
|
131 isClosed = true;
|
|
132 pooledCon.close(this);
|
|
133 }
|
|
134
|
|
135 public boolean isClosed() {
|
|
136 return isClosed;
|
|
137 }
|
|
138
|
|
139 }
|