001: package com.completex.objective.components.persistency.core.adapter;
002:
003: import com.completex.objective.components.log.Log;
004: import com.completex.objective.components.log.adapter.StdErrorLogAdapter;
005: import com.completex.objective.components.persistency.core.DatabasePolicy;
006: import com.completex.objective.components.persistency.transact.impl.PoolingDSTransactionManager;
007: import com.completex.objective.components.persistency.transact.impl.AbstractTransactionManager;
008: import com.completex.objective.components.cache.Cache;
009: import com.completex.objective.components.cache.impl.PreparedStatementCache;
010: import com.completex.objective.components.cache.impl.NullPreparedStatementCache;
011: import com.completex.objective.components.cache.impl.CallCache;
012: import com.completex.objective.components.cache.impl.NullCallCache;
013: import com.completex.objective.util.TypeUtil;
014:
015: import javax.sql.DataSource;
016:
017: /**
018: * @author Gennady Krizhevsky
019: */
020: public class CoreTransactionManagerFactory {
021:
022: protected static final Log DEFAULT_LOG = StdErrorLogAdapter
023: .newLogInstance();
024:
025: private DataSource dataSource;
026: private DatabasePolicy databasePolicy;
027: private int stmtCacheSize;
028: private boolean checkForBadConnection;
029: private Log logger;
030:
031: public CoreTransactionManagerFactory() {
032: setLogger(DEFAULT_LOG);
033: }
034:
035: public AbstractTransactionManager newTransactionManager() {
036: validateInit();
037: Cache stmtCache = prepareStatementCache(stmtCacheSize);
038: Cache callCache = prepareCallCache(stmtCacheSize);
039:
040: PoolingDSTransactionManager poolingDSTransactionManager = new PoolingDSTransactionManager(
041: logger, dataSource, stmtCache, callCache,
042: databasePolicy.connectionActiveSql());
043:
044: poolingDSTransactionManager
045: .setCheckForBadConnection(checkForBadConnection);
046: return poolingDSTransactionManager;
047: }
048:
049: static Cache prepareStatementCache(int stmtCacheSize) {
050: return stmtCacheSize > 0 ? ((Cache) new PreparedStatementCache(
051: stmtCacheSize)) : new NullPreparedStatementCache();
052: }
053:
054: static Cache prepareCallCache(int stmtCacheSize) {
055: return stmtCacheSize > 0 ? ((Cache) new CallCache(stmtCacheSize))
056: : new NullCallCache();
057: }
058:
059: protected void validateInit() {
060: TypeUtil.assertNotNull(dataSource, "DataSource");
061: TypeUtil.assertNotNull(databasePolicy, "DatabasePolicy");
062: }
063:
064: public DataSource getDataSource() {
065: return dataSource;
066: }
067:
068: public void setDataSource(DataSource dataSource) {
069: this .dataSource = dataSource;
070: }
071:
072: public DatabasePolicy getDatabasePolicy() {
073: return databasePolicy;
074: }
075:
076: public void setDatabasePolicy(DatabasePolicy databasePolicy) {
077: this .databasePolicy = databasePolicy;
078: }
079:
080: public int getStmtCacheSize() {
081: return stmtCacheSize;
082: }
083:
084: public void setStmtCacheSize(int stmtCacheSize) {
085: this .stmtCacheSize = stmtCacheSize;
086: }
087:
088: public boolean isCheckForBadConnection() {
089: return checkForBadConnection;
090: }
091:
092: public void setCheckForBadConnection(boolean checkForBadConnection) {
093: this .checkForBadConnection = checkForBadConnection;
094: }
095:
096: public Log getLogger() {
097: return DefaultDataSourceFactory.resolveLogger(logger);
098: }
099:
100: public void setLogger(Log logger) {
101: this.logger = DefaultDataSourceFactory.resolveLogger(logger);
102: }
103:
104: }
|