001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.transact.impl;
010:
011: import com.completex.objective.components.OdalRuntimeException;
012: import com.completex.objective.components.cache.Cache;
013: import com.completex.objective.components.log.Log;
014: import com.completex.objective.components.persistency.datasource.DataSourceIntf;
015: import com.completex.objective.components.persistency.transact.Transaction;
016:
017: import javax.sql.DataSource;
018: import java.sql.CallableStatement;
019: import java.sql.Connection;
020: import java.sql.PreparedStatement;
021: import java.sql.SQLException;
022:
023: /**
024: * TransactionManager implementation using external DataSource as connection pool
025: *
026: * @author Gennady Krizhevsky
027: */
028: public class PoolingDSTransactionManager extends
029: AbstractTransactionManager {
030: private DataSource dataSource;
031: private Cache statementCache;
032: private Cache callCache;
033: private String connectionValidationStatement;
034: private boolean localImplementation;
035:
036: protected PoolingDSTransactionManager() {
037: }
038:
039: /**
040: * @param log
041: * @param dataSource
042: * @param statementCache
043: * @param callCache
044: * @param connectionValidationStatement
045: */
046: public PoolingDSTransactionManager(Log log, DataSource dataSource,
047: Cache statementCache, Cache callCache,
048: String connectionValidationStatement) {
049:
050: super (log);
051: this .dataSource = dataSource;
052: if (dataSource instanceof DataSourceIntf) {
053: localImplementation = true;
054: setMaxAttempts(((DataSourceIntf) dataSource).getMaxSize());
055: }
056: this .statementCache = statementCache;
057: this .callCache = callCache;
058: this .connectionValidationStatement = connectionValidationStatement;
059: }
060:
061: protected Connection createConnection() throws SQLException {
062: return dataSource.getConnection();
063: }
064:
065: protected void destroyConnection(Connection connection)
066: throws SQLException {
067: connection.close();
068: }
069:
070: protected PreparedStatement prepareStatement(Connection connection,
071: String sql) throws SQLException {
072: return (PreparedStatement) statementCache.acquire(connection,
073: sql);
074: }
075:
076: protected void releaseStatement(Connection connection,
077: PreparedStatement statement) throws SQLException {
078: if (statement instanceof CallableStatement) {
079: callCache.release(connection, statement);
080: } else {
081: statementCache.release(connection, statement);
082: }
083: }
084:
085: protected CallableStatement prepareCall(Connection connection,
086: String sql) throws SQLException {
087: return (CallableStatement) callCache.acquire(connection, sql);
088: }
089:
090: protected void releaseBad(Connection connection) {
091: if (localImplementation) {
092: ((DataSourceIntf) dataSource).releaseBad(connection);
093: }
094: }
095:
096: public void setConnectionValidationStatement(
097: String connectionValidationStatement) {
098: this .connectionValidationStatement = connectionValidationStatement;
099: }
100:
101: public String getConnectionValidationStatement() {
102: return connectionValidationStatement;
103: }
104:
105: public void commit(Transaction transaction) throws SQLException {
106: try {
107: super .commit(transaction);
108: } finally {
109: release(transaction);
110: }
111: }
112:
113: public void rollback(Transaction transaction) throws SQLException {
114: try {
115: super .rollback(transaction);
116: } finally {
117: release(transaction);
118: }
119: }
120:
121: public Transaction beginUnchecked() {
122: try {
123: return begin();
124: } catch (SQLException e) {
125: throw new OdalRuntimeException(e.getClass().getName()
126: + ": " + e.getMessage());
127: }
128: }
129:
130: public void commitUnchecked(Transaction transaction) {
131: try {
132: commit(transaction);
133: } catch (SQLException e) {
134: throw new OdalRuntimeException(e.getClass().getName()
135: + ": " + e.getMessage());
136: }
137: }
138:
139: public void rollbackUnchecked(Transaction transaction) {
140: try {
141: rollback(transaction);
142: } catch (SQLException e) {
143: throw new OdalRuntimeException(e.getClass().getName()
144: + ": " + e.getMessage());
145: }
146: }
147:
148: public void releaseUnchecked(Transaction transaction) {
149: try {
150: release(transaction);
151: } catch (SQLException e) {
152: throw new OdalRuntimeException(e.getClass().getName()
153: + ": " + e.getMessage());
154: }
155: }
156:
157: public void rollbackSilently(Transaction transaction) {
158: if (transaction != null) {
159: try {
160: rollback(transaction);
161: } catch (Exception e) {
162: // Silently!
163: getLogger().error("", e);
164: }
165: }
166: }
167:
168: public void release(Transaction transaction) throws SQLException {
169: if (localImplementation) {
170: ((DataSourceIntf) dataSource).release(transaction
171: .getConnection());
172: } else {
173: destroyConnection(transaction.getConnection());
174: }
175: }
176:
177: public void shutdown() {
178: if (localImplementation) {
179: ((DataSourceIntf) dataSource).shutdown();
180: }
181: }
182:
183: public String toString() {
184: return super .toString() + ": " + dataSource;
185: }
186:
187: public DataSource getDataSource() {
188: return dataSource;
189: }
190:
191: public void setDataSource(DataSource dataSource) {
192: this .dataSource = dataSource;
193: }
194:
195: public Cache getStatementCache() {
196: return statementCache;
197: }
198:
199: public void setStatementCache(Cache statementCache) {
200: this .statementCache = statementCache;
201: }
202:
203: public Cache getCallCache() {
204: return callCache;
205: }
206:
207: public void setCallCache(Cache callCache) {
208: this.callCache = callCache;
209: }
210:
211: }
|