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.cache.Cache;
012: import com.completex.objective.components.log.Log;
013: import com.completex.objective.components.persistency.transact.Transaction;
014: import com.completex.objective.components.pool.Pool;
015: import com.completex.objective.components.OdalRuntimeException;
016:
017: import java.sql.CallableStatement;
018: import java.sql.Connection;
019: import java.sql.PreparedStatement;
020: import java.sql.SQLException;
021:
022: /**
023: * TransactionManager implementation using external Pool as connection pool
024: *
025: * @author Gennady Krizhevsky
026: */
027: public class PoolingTransactionManager extends
028: AbstractTransactionManager {
029: private Pool connectionPool;
030: private Cache statementCache;
031: private Cache callCache;
032: private String connectionValidationStatement;
033:
034: /**
035: * @param connectionPool
036: * @param statementCache Prepared statemets cache
037: * @param callCache Callable statements cache
038: * @param connectionValidationStatement
039: * @param log
040: */
041: public PoolingTransactionManager(Pool connectionPool,
042: Cache statementCache, Cache callCache,
043: String connectionValidationStatement, Log log) {
044: super (log);
045: this .connectionPool = connectionPool;
046: this .statementCache = statementCache;
047: this .callCache = callCache;
048: this .connectionValidationStatement = connectionValidationStatement;
049: setMaxAttempts(connectionPool.getMaxSize());
050: }
051:
052: public void setCheckForBadConnection(boolean checkForBadConnection) {
053: super .setCheckForBadConnection(checkForBadConnection);
054: }
055:
056: protected Connection createConnection() {
057: return (Connection) connectionPool.acquireResource();
058: }
059:
060: protected void destroyConnection(Connection connection) {
061: connectionPool.releaseResource(connection);
062: }
063:
064: protected PreparedStatement prepareStatement(Connection connection,
065: String sql) throws SQLException {
066: return (PreparedStatement) statementCache.acquire(connection,
067: sql);
068: }
069:
070: protected void releaseStatement(Connection connection,
071: PreparedStatement statement) throws SQLException {
072: if (statement instanceof CallableStatement) {
073: callCache.release(connection, statement);
074: } else {
075: statementCache.release(connection, statement);
076: }
077: }
078:
079: protected CallableStatement prepareCall(Connection connection,
080: String sql) throws SQLException {
081: return (CallableStatement) callCache.acquire(connection, sql);
082: }
083:
084: protected String getConnectionValidationStatement() {
085: return connectionValidationStatement;
086: }
087:
088: protected void releaseBad(Connection connection) {
089: connectionPool.releaseBadResource(connection);
090: }
091:
092: public void commit(Transaction transaction) throws SQLException {
093: try {
094: super .commit(transaction);
095: } finally {
096: connectionPool.releaseResource(transaction.getConnection());
097: }
098: }
099:
100: public void rollback(Transaction transaction) throws SQLException {
101: try {
102: super .rollback(transaction);
103: } finally {
104: connectionPool.releaseResource(transaction.getConnection());
105: }
106: }
107:
108: public Transaction beginUnchecked() {
109: try {
110: return begin();
111: } catch (SQLException e) {
112: throw new OdalRuntimeException(e.getClass().getName()
113: + ": " + e.getMessage());
114: }
115: }
116:
117: public void commitUnchecked(Transaction transaction) {
118: try {
119: commit(transaction);
120: } catch (SQLException e) {
121: throw new OdalRuntimeException(e.getClass().getName()
122: + ": " + e.getMessage());
123: }
124: }
125:
126: public void rollbackUnchecked(Transaction transaction) {
127: try {
128: rollback(transaction);
129: } catch (SQLException e) {
130: throw new OdalRuntimeException(e.getClass().getName()
131: + ": " + e.getMessage());
132: }
133: }
134:
135: public void releaseUnchecked(Transaction transaction) {
136: release(transaction);
137: }
138:
139: public void rollbackSilently(Transaction transaction) {
140: if (transaction != null) {
141: try {
142: rollback(transaction);
143: } catch (Exception e) {
144: // Silently!
145: getLogger().error("", e);
146: }
147: }
148: }
149:
150: public void release(Transaction transaction) {
151: connectionPool.releaseResource(transaction.getConnection());
152: }
153:
154: public void shutdown() {
155: connectionPool.shutdown();
156: }
157:
158: public String toString() {
159: return super .toString() + ": " + connectionPool;
160: }
161: }
|