KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > jdbc > conn > ExternalJdbcConnectionSource


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.jdbc.conn;
13
14 import com.versant.core.jdbc.JdbcConnectionSource;
15 import com.versant.core.logging.LogEventStore;
16
17 import javax.sql.DataSource JavaDoc;
18 import java.sql.Connection JavaDoc;
19 import java.sql.SQLException JavaDoc;
20
21 /**
22  * Gets connections from one or more java.sql.DataSource's and/or a
23  * JDBCConnectionPool. If there are 2 DataSource's then the second is used
24  * for highPriority (keygen) connections. If there is only one DataSource
25  * and a pool then the pool is used for highPriority connections. Otherwise
26  * the first DataSource is used.
27  * <p/>
28  * Connections obtained from the DataSource(s) may optionally have
29  * PreparedStatement pooling enabled for them. Operations on these connections
30  * will also be logged depending on the logging level.
31  */

32 public class ExternalJdbcConnectionSource implements JdbcConnectionSource {
33
34     private DataSource JavaDoc ds1;
35     private DataSource JavaDoc ds2;
36     private JDBCConnectionPool pool;
37     private LogEventStore pes;
38     private String JavaDoc url;
39     private String JavaDoc driverName;
40
41     private boolean usePsPool;
42     private int psCacheMax;
43     private boolean clearBatch;
44
45     public ExternalJdbcConnectionSource(DataSource JavaDoc ds1, DataSource JavaDoc ds2,
46             JDBCConnectionPool pool, LogEventStore pes) {
47         this.ds1 = ds1;
48         this.ds2 = ds2;
49         this.pool = pool;
50         this.pes = pes;
51     }
52
53     public void setUrl(String JavaDoc url) {
54         this.url = url;
55     }
56
57     public void setDriverName(String JavaDoc driverName) {
58         this.driverName = driverName;
59     }
60
61     public boolean isUsePsPool() {
62         return usePsPool;
63     }
64
65     /**
66      * Use PreparedStatement pooling on connections obtained from DataSource's
67      * or not.
68      */

69     public void setUsePsPool(boolean usePsPool) {
70         this.usePsPool = usePsPool;
71     }
72
73     public int getPsCacheMax() {
74         return psCacheMax;
75     }
76
77     /**
78      * Limit the number of cached PreparedStatement's per connection to this
79      * if PreparedStatement pooling is enabled.
80      */

81     public void setPsCacheMax(int psCacheMax) {
82         this.psCacheMax = psCacheMax;
83     }
84
85     public boolean isClearBatch() {
86         return clearBatch;
87     }
88
89     /**
90      * Invoke clearBatch on PreparedStatements returned to the pool
91      * if PreparedStatement pooling is enabled.
92      */

93     public void setClearBatch(boolean clearBatch) {
94         this.clearBatch = clearBatch;
95     }
96
97     public Connection JavaDoc getConnection(boolean highPriority, boolean autoCommit)
98             throws SQLException JavaDoc {
99         if (highPriority) {
100             if (pool != null) {
101                 return pool.getConnection(false, autoCommit);
102             } else if (ds2 != null) {
103                 return getConnection(ds2);
104             }
105         }
106         return getConnection(ds1);
107     }
108
109     /**
110      * Get a Connection from ds and wrap it.
111      */

112     protected Connection JavaDoc getConnection(DataSource JavaDoc ds) throws SQLException JavaDoc {
113         return new LoggingConnection(ds.getConnection(), pes, usePsPool,
114                 psCacheMax, clearBatch);
115     }
116
117     public void returnConnection(Connection JavaDoc con) throws SQLException JavaDoc {
118         if (con instanceof PooledConnection) {
119             pool.returnConnection(con);
120         } else {
121             con.close();
122         }
123     }
124
125     public String JavaDoc getURL() {
126         return url;
127     }
128
129     public String JavaDoc getDriverName() {
130         return driverName;
131     }
132
133     public void init() {
134         if (pool != null) {
135             pool.init();
136         }
137     }
138
139     public void destroy() {
140         ds1 = ds2 = null;
141         if (pool != null) {
142             pool.destroy();
143             pool = null;
144         }
145     }
146
147     public void closeIdleConnections() {
148         if (pool != null) {
149             pool.closeIdleConnections();
150         }
151     }
152
153 }
154
155
Popular Tags