1 12 13 package org.openbravo.database; 14 import java.sql.*; 15 import javax.sql.DataSource ; 16 import javax.naming.Context ; 17 import javax.naming.InitialContext ; 18 import javax.naming.NamingException ; 19 import javax.servlet.*; 20 import org.apache.log4j.Logger; 21 import org.apache.commons.dbcp.ConnectionFactory; 22 import org.apache.commons.dbcp.PoolingDataSource; 23 import org.apache.commons.dbcp.PoolableConnectionFactory; 24 import org.apache.commons.pool.KeyedObjectPoolFactory; 25 import org.apache.commons.pool.impl.StackKeyedObjectPoolFactory; 26 import org.apache.commons.pool.impl.GenericObjectPool; 27 import org.openbravo.database.OpenBravoDriverManagerConnectionFactory; 28 29 36 public class ConnectionPool { 37 static Logger log4j = Logger.getLogger(ConnectionPool.class); 38 private DataSource myPool; 39 private String rdbms = ""; 40 41 public ConnectionPool(String namespace) throws ServletException { 42 try { 43 Context initContext = new InitialContext (); 44 Context envContext = (Context )initContext.lookup("java:/comp/env"); 45 myPool = (DataSource )envContext.lookup(namespace); 46 } catch (NamingException nex) { 47 nex.printStackTrace(); 48 throw new ServletException(nex.toString()); 49 } 50 log4j.info("Starting ConnectionPool Version 1.0.1:"); 51 } 53 public ConnectionPool(String _driver, String _server, String _login, String _password, int _minConns, int _maxConns, double _maxConnTime, String _dbSessionConfig, String _rdbms) throws ServletException { 54 rdbms = _rdbms; 55 if (log4j.isDebugEnabled()) log4j.debug("Loading underlying JDBC driver."); 56 try { 57 Class.forName(_driver); 58 } catch (ClassNotFoundException e) { 59 throw new ServletException(e.toString()); 60 } 61 if (log4j.isDebugEnabled()) log4j.debug("Done."); 62 63 64 GenericObjectPool connectionPool = new GenericObjectPool(null); 65 connectionPool.setWhenExhaustedAction(GenericObjectPool.WHEN_EXHAUSTED_GROW); 66 connectionPool.setMaxActive(_maxConns); 67 connectionPool.setTestOnBorrow(false); 68 connectionPool.setTestOnReturn(false); 69 connectionPool.setTestWhileIdle(false); 70 71 KeyedObjectPoolFactory keyedObject = new StackKeyedObjectPoolFactory(); 72 ConnectionFactory connectionFactory = new OpenBravoDriverManagerConnectionFactory(_server, _login, _password, _dbSessionConfig); 73 PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,connectionPool,keyedObject, 74 null,false,true); 75 76 PoolingDataSource dataSource = new PoolingDataSource(connectionPool); 77 78 myPool = dataSource; 79 } 80 81 82 87 public Connection getConnection() { 88 Connection conn=null; 89 try { 90 conn = myPool.getConnection(); 91 } catch (SQLException ex) { 92 ex.printStackTrace(); 93 } 94 return conn; 95 } 96 97 public String getRDBMS() { 98 return rdbms; 99 } 100 101 105 public boolean releaseConnection(Connection conn) { 106 try { 107 conn.setAutoCommit(true); 108 conn.close(); 109 } catch (SQLException e) { 110 e.printStackTrace(); 111 } 112 return true; 113 } 114 115 public PreparedStatement getPreparedStatement(String SQLPreparedStatement) { 116 if (log4j.isDebugEnabled()) log4j.debug("connection requested"); 117 Connection conn = getConnection(); 118 if (log4j.isDebugEnabled()) log4j.debug("connection established"); 119 return getPreparedStatement(conn, SQLPreparedStatement); 120 } 121 122 public PreparedStatement getPreparedStatement(Connection conn, String SQLPreparedStatement) { 123 try { 124 PreparedStatement ps; 125 if (log4j.isDebugEnabled()) log4j.debug("preparedStatement requested"); 126 ps = conn.prepareStatement(SQLPreparedStatement, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); 127 if (log4j.isDebugEnabled()) log4j.debug("preparedStatement received"); 128 return(ps); 129 } catch(SQLException e) { 130 log4j.error("getPreparedStatement: " + SQLPreparedStatement); 131 releaseConnection(conn); 132 e.printStackTrace(); 133 return null; 134 } 135 } 136 137 public CallableStatement getCallableStatement(String SQLCallableStatement) { 138 Connection conn = getConnection(); 139 return getCallableStatement(conn, SQLCallableStatement); 140 } 141 142 public CallableStatement getCallableStatement(Connection conn, String SQLCallableStatement) { 143 try { 144 CallableStatement cs; 145 cs = conn.prepareCall(SQLCallableStatement); 146 return(cs); 147 } catch(SQLException e) { 148 log4j.error("getCallableStatement: " + SQLCallableStatement); 149 releaseConnection(conn); 150 e.printStackTrace(); 151 return null; 152 } 153 } 154 155 public Statement getStatement() { 156 Connection conn = getConnection(); 157 return getStatement(conn); 158 } 159 160 public Statement getStatement(Connection conn) { 161 try { 162 return(conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY)); 163 } catch(SQLException e) { 164 releaseConnection(conn); 165 e.printStackTrace(); 166 return null; 167 } 168 } 169 170 public void releasePreparedStatement(PreparedStatement preparedStatement) { 171 try { 172 Connection conn = preparedStatement.getConnection(); 173 releaseConnection(conn); 174 } catch (SQLException e) { 175 e.printStackTrace(); 176 } 177 } 178 179 public void releaseCallableStatement(CallableStatement callableStatement) { 180 try { 181 Connection conn = callableStatement.getConnection(); 182 releaseConnection(conn); 183 } catch (SQLException e) { 184 e.printStackTrace(); 185 } 186 } 187 188 public void releaseStatement(Statement statement) { 189 try { 190 Connection conn = statement.getConnection(); 191 statement.close(); 192 releaseConnection(conn); 193 } catch (SQLException e) { 194 e.printStackTrace(); 195 } 196 } 197 198 199 203 public void destroy() { 204 myPool = null; 205 } 206 207 210 public String getStatus() { 211 StringBuffer strResultado = new StringBuffer (); 212 strResultado.append("Not implemented yet"); 213 return strResultado.toString(); 214 }} 216 | Popular Tags |