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 fschmidt.db.DbDatabase;
|
|
26 import fschmidt.db.SQLRuntimeException;
|
|
27 import fschmidt.db.extend.DbDatabaseExt;
|
|
28 import fschmidt.db.extend.DbTransaction;
|
|
29 import fschmidt.db.extend.FilterDatabase;
|
|
30 import org.slf4j.Logger;
|
|
31 import org.slf4j.LoggerFactory;
|
|
32
|
|
33 import java.sql.Array;
|
|
34 import java.sql.Blob;
|
|
35 import java.sql.CallableStatement;
|
|
36 import java.sql.Clob;
|
|
37 import java.sql.Connection;
|
|
38 import java.sql.DatabaseMetaData;
|
|
39 import java.sql.NClob;
|
|
40 import java.sql.PreparedStatement;
|
|
41 import java.sql.SQLClientInfoException;
|
|
42 import java.sql.SQLException;
|
|
43 import java.sql.SQLWarning;
|
|
44 import java.sql.SQLXML;
|
|
45 import java.sql.Savepoint;
|
|
46 import java.sql.Statement;
|
|
47 import java.sql.Struct;
|
|
48 import java.util.Map;
|
|
49 import java.util.Properties;
|
|
50
|
|
51
|
|
52 public class DbDatabaseImpl extends FilterDatabase {
|
|
53 private static final Logger logger = LoggerFactory.getLogger(DbDatabaseImpl.class);
|
|
54
|
|
55 final Pool pool;
|
|
56 final String user;
|
|
57
|
|
58 public DbDatabaseImpl(DbDatabase database,Pool pool,String user) {
|
|
59 super((DbDatabaseExt)database);
|
|
60 this.pool = pool;
|
|
61 this.user = user;
|
|
62 }
|
|
63
|
|
64 public DbDatabaseImpl(DbDatabase database) {
|
|
65 this(database,new Pool(),null);
|
|
66 }
|
|
67
|
|
68 DbDatabase database() {
|
|
69 return database;
|
|
70 }
|
|
71
|
|
72 @Override public boolean isInTransaction() {
|
|
73 return pool.isInTransaction();
|
|
74 }
|
|
75
|
|
76 @Override public void beginTransaction()
|
|
77 throws SQLRuntimeException
|
|
78 {
|
|
79 if( isInTransaction() )
|
|
80 throw new IllegalStateException("beginTransaction called in nested transaction");
|
|
81 try {
|
|
82 Connection con = getConnection();
|
|
83 con.setAutoCommit(false);
|
|
84 } catch(SQLException e) {
|
|
85 throw new SQLRuntimeException(e);
|
|
86 }
|
|
87 }
|
|
88
|
|
89 @Override public void commitTransaction() {
|
|
90 pool.commitTransaction();
|
|
91 }
|
|
92
|
|
93 @Override public void endTransaction() {
|
|
94 pool.endTransaction();
|
|
95 }
|
|
96
|
|
97 @Override public void runBeforeCommit(Runnable r) {
|
|
98 if( isInTransaction() ) {
|
|
99 pool.runBeforeCommit(r);
|
|
100 } else {
|
|
101 super.runBeforeCommit(r);
|
|
102 }
|
|
103 }
|
|
104
|
|
105 @Override public void runJustAfterCommit(Runnable r) {
|
|
106 if( isInTransaction() ) {
|
|
107 pool.runJustAfterCommit(r);
|
|
108 } else {
|
|
109 super.runJustAfterCommit(r);
|
|
110 }
|
|
111 }
|
|
112
|
|
113 @Override public void runAfterCommit(Runnable r) {
|
|
114 if( isInTransaction() ) {
|
|
115 pool.runAfterCommit(r);
|
|
116 } else {
|
|
117 super.runAfterCommit(r);
|
|
118 }
|
|
119 }
|
|
120
|
|
121 @Override public void ignoreRunnablesInThisTransaction() {
|
|
122 pool.ignoreRunnablesInThisTransaction();
|
|
123 }
|
|
124
|
|
125 @Override public DbTransaction getTransaction() {
|
|
126 return pool.getTransaction();
|
|
127 }
|
|
128
|
|
129 @Override public Connection getConnection()
|
|
130 throws SQLException
|
|
131 {
|
|
132 return pool.getConnection(this);
|
|
133 }
|
|
134 }
|