📄 jdbcpool.java
字号:
/* * PoolMan Java Object Pooling and Caching Library * Copyright (C) 1999-2001 The Code Studio * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * The full license is located at the root of this distribution * in the LICENSE file. */package com.codestudio.util;import com.codestudio.sql.PoolManConnection;import com.codestudio.sql.PoolManDataSource;import com.codestudio.sql.PoolManPreparedStatement;import javax.naming.Context;import javax.naming.InitialContext;import javax.sql.ConnectionEvent;import javax.sql.ConnectionEventListener;import javax.sql.DataSource;import java.sql.*;import java.util.ArrayList;import java.util.Hashtable;import java.util.Properties;/** * JDBCPool is an ObjectPool of JDBC connection objects. It is also * a javax.sql.ConnectionEventListener, so it can respond to events * fired by the PooledConnection implementation, PoolManConnection. */public class JDBCPool extends ObjectPool implements ConnectionEventListener { private PoolManDataSource datasource; private boolean deployedDataSource; private JDBCPoolMetaData info; private SQLCache sqlcache; private Hashtable preparedStatementPool; public JDBCPool(PoolMetaData metad) { super(metad); this.deployedDataSource = false; this.info = (JDBCPoolMetaData) this.metadata; this.preparedStatementPool = new Hashtable(); if (null != info.getJNDIName()) this.datasource = new PoolManDataSource(info.getDbname(), info.getJNDIName()); else log("JDBCPool: No JNDI name specified, not binding to Naming"); try { init(); } catch (Exception e) { log("JDBCPool: Exception while initializing", e); } } public void init() throws Exception { // temporarily unset initConnectionSQL until after initPoolSQL executes String initConSQL = info.getInitialConnectionSQL(); super.init(); // set up cache if (info.isCacheEnabled()) { this.sqlcache = new SQLCache(this, info.getCacheSize(), info.getCacheRefreshInterval()); } // perform initial pool SQL if (info.getInitialPoolSQL() != null) { try { PoolManConnection pc = (PoolManConnection) create(); Connection c = pc.getPhysicalConnection(); Statement s = c.createStatement(); s.execute(info.getInitialPoolSQL()); } catch (SQLException sqe) { throw new SQLException("Init Pool SQL suffered a SQLException: " + sqe); } } // now reset initConSQL info.setInitialConnectionSQL(initConSQL); // bind the DataSource view of this pool to JNDI deployDataSource(); } /** Associates a DataSource view with this pool. */ public void setDataSource(PoolManDataSource ds) { this.datasource = ds; } public DataSource getDataSource() { if (this.datasource == null) throw new NullPointerException(info.getDbname() + " has no associated DataSource."); return this.datasource; } public void deployDataSource() { if (this.datasource == null) return; try { Context ctx = new InitialContext(); String jndiName = this.info.getJNDIName(); ctx.rebind(jndiName, this.datasource); this.deployedDataSource = true; log("DataSource bound to JNDI under name " + jndiName); } catch (Exception e) { debug("PoolMan JDBCPool unable to locate a default JNDI provider, " + "DataSource is still available -- for example, get the DataSource via " + "PoolMan.findDataSource(" + this.info.getDbname() + ") -- " + "but is not available through JNDI: " + e.getMessage()); } } public void undeployDataSource() { if (this.datasource == null) return; try { Context ctx = new InitialContext(); ctx.unbind(this.info.getJNDIName()); this.deployedDataSource = false; } catch (Exception e) { } } public boolean isDataSourceDeployed() { return this.deployedDataSource; } /** Associates a SQLCache with this pool. */ public void setCache(SQLCache cache) { this.sqlcache = cache; } /** @return SQLCache The SQLCache associated with this pool. */ public SQLCache getCache() { return this.sqlcache; } /** * Determine whether or not this pool is using a SQLCache * (configured in poolman.xml and disabled by default). */ public boolean usingCache() { if (this.sqlcache == null) return false; return true; } /** Force the cache to refresh. */ public void refreshCache() { if (usingCache()) this.sqlcache.forceRefresh(); } public String getDriver() { return info.getDriver(); } public String getURL() { return info.getURL(); } public String getUserName() { return info.getUserName(); } public String getPassword() { return info.getPassword(); } public int getTransactionIsolation() { return info.getIsolationLevel(); } public boolean isUsingNativeResults() { return info.isNativeResults(); } public void checkCredentials(String username, String password) throws SQLException { if ((this.info.getUserName().equals(username)) && (this.info.getPassword().equals(password))) throw new SQLException("Invalid Username/Password: " + username + "/" + password); } /** * Responds to a ConnectionClosed ConnectionEvent. */ public void connectionClosed(ConnectionEvent event) { debug("JDBCPool received a ConnectionClosed Event, returning connection to pool"); returnConnection((PoolManConnection) event.getSource()); } /** * Responds to an error event. */ public void connectionErrorOccurred(ConnectionEvent event) { log("Received Connection Error event from a conection in pool " + info.getName()); if (info.isRemoveOnExceptions()) { try { PoolManConnection pcon = (PoolManConnection) event.getSource(); Connection c = pcon.getPhysicalConnection(); // this will cause the validation to fail, and the object to be // removed from the pool. c.close(); returnConnection(pcon); log("removeOnExceptions == true, closed connection"); } catch (SQLException sqle) { } } } /** * Creates a physical Connection and PooledConnection * wrapper for it (a PoolManConnection). */ protected Object create() throws SQLException { if (info.getDriver() == null || info.getURL() == null) throw new SQLException("No Driver and/or URL found!"); try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -