001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.jdbc.conn;
012:
013: import com.versant.core.jdbc.JdbcConnectionSource;
014: import com.versant.core.logging.LogEventStore;
015:
016: import javax.sql.DataSource;
017: import java.sql.Connection;
018: import java.sql.SQLException;
019:
020: /**
021: * Gets connections from one or more java.sql.DataSource's and/or a
022: * JDBCConnectionPool. If there are 2 DataSource's then the second is used
023: * for highPriority (keygen) connections. If there is only one DataSource
024: * and a pool then the pool is used for highPriority connections. Otherwise
025: * the first DataSource is used.
026: * <p/>
027: * Connections obtained from the DataSource(s) may optionally have
028: * PreparedStatement pooling enabled for them. Operations on these connections
029: * will also be logged depending on the logging level.
030: */
031: public class ExternalJdbcConnectionSource implements
032: JdbcConnectionSource {
033:
034: private DataSource ds1;
035: private DataSource ds2;
036: private JDBCConnectionPool pool;
037: private LogEventStore pes;
038: private String url;
039: private String driverName;
040:
041: private boolean usePsPool;
042: private int psCacheMax;
043: private boolean clearBatch;
044:
045: public ExternalJdbcConnectionSource(DataSource ds1, DataSource ds2,
046: JDBCConnectionPool pool, LogEventStore pes) {
047: this .ds1 = ds1;
048: this .ds2 = ds2;
049: this .pool = pool;
050: this .pes = pes;
051: }
052:
053: public void setUrl(String url) {
054: this .url = url;
055: }
056:
057: public void setDriverName(String driverName) {
058: this .driverName = driverName;
059: }
060:
061: public boolean isUsePsPool() {
062: return usePsPool;
063: }
064:
065: /**
066: * Use PreparedStatement pooling on connections obtained from DataSource's
067: * or not.
068: */
069: public void setUsePsPool(boolean usePsPool) {
070: this .usePsPool = usePsPool;
071: }
072:
073: public int getPsCacheMax() {
074: return psCacheMax;
075: }
076:
077: /**
078: * Limit the number of cached PreparedStatement's per connection to this
079: * if PreparedStatement pooling is enabled.
080: */
081: public void setPsCacheMax(int psCacheMax) {
082: this .psCacheMax = psCacheMax;
083: }
084:
085: public boolean isClearBatch() {
086: return clearBatch;
087: }
088:
089: /**
090: * Invoke clearBatch on PreparedStatements returned to the pool
091: * if PreparedStatement pooling is enabled.
092: */
093: public void setClearBatch(boolean clearBatch) {
094: this .clearBatch = clearBatch;
095: }
096:
097: public Connection getConnection(boolean highPriority,
098: boolean autoCommit) throws SQLException {
099: 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 getConnection(DataSource ds)
113: throws SQLException {
114: return new LoggingConnection(ds.getConnection(), pes,
115: usePsPool, psCacheMax, clearBatch);
116: }
117:
118: public void returnConnection(Connection con) throws SQLException {
119: if (con instanceof PooledConnection) {
120: pool.returnConnection(con);
121: } else {
122: con.close();
123: }
124: }
125:
126: public String getURL() {
127: return url;
128: }
129:
130: public String getDriverName() {
131: return driverName;
132: }
133:
134: public void init() {
135: if (pool != null) {
136: pool.init();
137: }
138: }
139:
140: public void destroy() {
141: ds1 = ds2 = null;
142: if (pool != null) {
143: pool.destroy();
144: pool = null;
145: }
146: }
147:
148: public void closeIdleConnections() {
149: if (pool != null) {
150: pool.closeIdleConnections();
151: }
152: }
153:
154: }
|