1 2 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 ; 18 import java.sql.Connection ; 19 import java.sql.SQLException ; 20 21 32 public class ExternalJdbcConnectionSource implements JdbcConnectionSource { 33 34 private DataSource ds1; 35 private DataSource ds2; 36 private JDBCConnectionPool pool; 37 private LogEventStore pes; 38 private String url; 39 private String driverName; 40 41 private boolean usePsPool; 42 private int psCacheMax; 43 private boolean clearBatch; 44 45 public ExternalJdbcConnectionSource(DataSource ds1, DataSource 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 url) { 54 this.url = url; 55 } 56 57 public void setDriverName(String driverName) { 58 this.driverName = driverName; 59 } 60 61 public boolean isUsePsPool() { 62 return usePsPool; 63 } 64 65 69 public void setUsePsPool(boolean usePsPool) { 70 this.usePsPool = usePsPool; 71 } 72 73 public int getPsCacheMax() { 74 return psCacheMax; 75 } 76 77 81 public void setPsCacheMax(int psCacheMax) { 82 this.psCacheMax = psCacheMax; 83 } 84 85 public boolean isClearBatch() { 86 return clearBatch; 87 } 88 89 93 public void setClearBatch(boolean clearBatch) { 94 this.clearBatch = clearBatch; 95 } 96 97 public Connection getConnection(boolean highPriority, boolean autoCommit) 98 throws SQLException { 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 112 protected Connection getConnection(DataSource ds) throws SQLException { 113 return new LoggingConnection(ds.getConnection(), pes, usePsPool, 114 psCacheMax, clearBatch); 115 } 116 117 public void returnConnection(Connection con) throws SQLException { 118 if (con instanceof PooledConnection) { 119 pool.returnConnection(con); 120 } else { 121 con.close(); 122 } 123 } 124 125 public String getURL() { 126 return url; 127 } 128 129 public String 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 |