poolmandatasource.java
来自「Java Database connection pool」· Java 代码 · 共 150 行
JAVA
150 行
/* * 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.sql;// The JDK and Extensionsimport com.codestudio.util.SQLManager;import javax.naming.Reference;import javax.naming.Referenceable;import javax.naming.Name;import javax.naming.Context;import javax.naming.StringRefAddr;import javax.naming.spi.ObjectFactory;import javax.sql.ConnectionPoolDataSource;import javax.sql.DataSource;import javax.sql.PooledConnection;import java.io.PrintWriter;import java.sql.Connection;import java.sql.SQLException;import java.util.Hashtable;public class PoolManDataSource implements DataSource, ConnectionPoolDataSource, Referenceable, ObjectFactory { private String poolName; private String jndiName; private PrintWriter logger; private int loginTimeout; public PoolManDataSource() { } public PoolManDataSource(String poolName, String jndiName) { this(); this.poolName = poolName; this.jndiName = jndiName; } public String getPoolName() { return this.poolName; } public String getJNDIName() { return this.jndiName; } /* DATASOURCE METHODS */ public Connection getConnection() throws SQLException { return SQLManager.getInstance().requestConnection(this.poolName); } public Connection getConnection(String user, String password) throws SQLException { SQLManager.getInstance().checkCredentials(this.poolName, user, password); return getConnection(); } /* CONNECTIONPOOL DATASOURCE METHODS */ public javax.sql.PooledConnection getPooledConnection() throws SQLException { return (PooledConnection) getConnection(); } public javax.sql.PooledConnection getPooledConnection(String user, String password) throws SQLException { return (PooledConnection) getConnection(user, password); } /* COMMON DATASOURCE METHODS */ public void setLoginTimeout(int seconds) throws SQLException { this.loginTimeout = seconds; } public int getLoginTimeout() throws SQLException { return this.loginTimeout; } public void setLogWriter(PrintWriter out) throws SQLException { this.logger = out; } public PrintWriter getLogWriter() throws SQLException { return this.logger; }
/* OBJECT METHODS */
public String toString() { return "PoolManDataSource:[JNDIName:" + jndiName + ",PoolName:" + poolName + "]"; } /* JNDI REFERENCE METHODS */ public Reference getReference() { Reference ref = new Reference("com.codestudio.sql.PoolManDataSource", "com.codestudio.sql.PoolManDataSource", null); ref.add(new StringRefAddr("dbname", this.poolName)); ref.add(new StringRefAddr("jndiname", this.jndiName)); return ref; } public Object getObjectInstance(Object RefObj, Name Nm, Context Ctx, Hashtable Env) throws Exception { Object result = null; Reference ref = (Reference) RefObj; if (ref.getClassName().equals("com.codestudio.sql.PoolManDataSource")) { String poolname = (String) ref.get("dbname").getContent(); String jndiname = (String) ref.get("jndiname").getContent(); PoolManDataSource pds = new PoolManDataSource(poolname, jndiname); result = pds; } return result; }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?