1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.core.database;
17
18 import java.sql.Connection;
19 import java.sql.SQLException;
20 import java.util.Hashtable;
21
22 import javax.naming.Context;
23 import javax.naming.Name;
24
25 import org.apache.commons.dbcp.PoolingConnection;
26 import org.apache.commons.pool.KeyedObjectPool;
27 import org.apache.commons.pool.KeyedObjectPoolFactory;
28 import org.apache.commons.pool.impl.GenericKeyedObjectPool;
29 import org.apache.commons.pool.impl.GenericKeyedObjectPoolFactory;
30 import org.enhydra.jdbc.standard.StandardXADataSource;
31 import org.enhydra.jdbc.standard.StandardXAStatefulConnection;
32
33
34
35
36
37
38
39
40
41 public class RiceXADataSource extends StandardXADataSource {
42 private KeyedObjectPoolFactory _stmtPoolFactory;
43 private int preparedStatementCacheSize;
44
45 public RiceXADataSource() {
46
47
48 super.setPreparedStmtCacheSize(0);
49 }
50
51 @Override
52 public synchronized Connection getConnection(String arg0, String arg1)
53 throws SQLException {
54 Connection conn = super.getConnection(arg0, arg1);
55
56
57 if (getPreparedStatementCacheSize() > 0) {
58 conn = wrapConnection(conn);
59 }
60 return conn;
61 }
62
63 @Override
64 public synchronized StandardXAStatefulConnection getFreeConnection() throws SQLException {
65 StandardXAStatefulConnection conn = super.getFreeConnection();
66 if (getPreparedStatementCacheSize() > 0) {
67 if (conn != null && !(conn.con instanceof PreparedStatementCachingConnection)) {
68 conn.con = wrapConnection(conn.con);
69 }
70 }
71 return conn;
72 }
73
74 public int getPreparedStatementCacheSize() {
75 return preparedStatementCacheSize;
76 }
77
78 public void setPreparedStatementCacheSize(int preparedStmtCacheSize) {
79 this.preparedStatementCacheSize = preparedStmtCacheSize;
80 }
81
82
83
84
85
86
87
88
89
90 @Override
91 public void setPreparedStmtCacheSize(int preparedStmtCacheSize) {
92
93
94 setPreparedStatementCacheSize(preparedStmtCacheSize);
95 }
96
97 protected KeyedObjectPoolFactory createStatementPoolFactory() {
98 return new GenericKeyedObjectPoolFactory(null,
99 -1,
100 GenericKeyedObjectPool.WHEN_EXHAUSTED_GROW,
101 0,
102 1,
103 getPreparedStatementCacheSize());
104 }
105
106 protected PreparedStatementCachingConnection wrapConnection(Connection realConnection) {
107
108 if (_stmtPoolFactory == null) {
109 _stmtPoolFactory = createStatementPoolFactory();
110 }
111
112 KeyedObjectPool stmtpool = _stmtPoolFactory.createPool();
113 PreparedStatementCachingConnection wrappedConnection = new PreparedStatementCachingConnection(realConnection, stmtpool);
114
115 stmtpool.setFactory(wrappedConnection);
116 return wrappedConnection;
117 }
118 }