一个Oracle数据库链接的JavaBean实例

- 编辑:admin -

一个Oracle数据库链接的JavaBean实例

a new connection is * created. * * @param name The pool name as defined in the properties file * @return Connection The connection or null */ public Connection getConnection(String name) { DBConnectionPool pool = (DBConnectionPool) pools.get(name); if (pool != null) { return pool.getConnection(); } return null; } /** * Returns an open connection. If no one is available, and the max * number of connections has not been reached, String passwd, maxconn); } /** * Loads and registers all JDBC drivers. This is done by the * DBConnectionManager。

creating one if its the * first time this method is called. * * @return DBConnectionManager The single instance. */ static synchronized public DBConnectionManager getInstance() { if (instance == null) { instance = new DBConnectionManager(); } clients++; return instance; } /** * A private constructor since this is a Singleton */ private DBConnectionManager() { // init(); } /** * Returns a connection to the named pool. * * @param name The pool name as defined in the properties file * @param con The Connection */ public void freeConnection(String name。

url, String driver, password。

password); } log("Created a new connection in pool " + name); } catch (SQLException e) { log(e, driver, max); pools.put(poolName, true); } catch (IOException e) { System.err.println("Cant open the log file log = new PrintWriter(System.err); } */ log = new PrintWriter(System.err); loadDrivers(driver); createPools(poolName。

Connection con) { DBConnectionPool pool = (DBConnectionPool) pools.get(name); if (pool != null) { pool.freeConnection(con); } } /** * Returns an open connection. If no one is available, String password, JDBCDriverType+":@110.7.6.22:1521:egong", waits until one * is available or the specified time has elapsed. * * @param name The pool name as defined in the properties file * @param time The number of milliseconds to wait * @return Connection The connection or null */ public Connection getConnection(String name, * since many pools may share the same driver. * * @param props The connection pool properties */ private void loadDrivers(String driverClassName) { /* String driverClasses = props.getProperty("drivers"); StringTokenizer st = new StringTokenizer(driverClasses); while (st.hasMoreElements()) { String driverClassName = st.nextToken().trim(); try { Driver driver = (Driver) Class.forName(driverClassName).newInstance(); DriverManager.registerDriver(driver); drivers.addElement(driver); log("Registered JDBC driver " + driverClassName); } catch (Exception e) { log("Cant register JDBC driver: " + driverClassName + ",。

String password, max); pools.put(poolName,conn); } } public class DBConnectionManager { static private DBConnectionManager instance=null; // The single instance static public int clients=0; private Vector drivers = new Vector(); private PrintWriter log; private Hashtable pools = new Hashtable(); private final int MAX_CONN = 1000; /** * Returns the single instance, String user, as opposed to the DBConnectionPool, String passwd) { init(poolName。

its removed from the pool * and this method is called again recursively. */ public synchronized Connection getConnection() { Connection con = null; if (freeConnections.size() 0) { // Pick the first Connection in the Vector // to get round-robin usage con = (Connection) freeConnections.firstElement(); freeConnections.removeElementAt(0); try { if (con.isClosed()) { log("Removed bad connection from " + name); // Try again recursively con = getConnection(); } } catch (SQLException e) { log("Removed bad connection from " + name); // Try again recursively con = getConnection(); } } else if (maxConn == 0 || checkedOut maxConn) { con = newConnection(); } if (con != null) { checkedOut++; } return con; }