📄 datasource.java
字号:
//
//
package com.scxh.eei.sql;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Properties;
import java.util.Vector;
import java.util.Hashtable;
import javax.naming.NamingException;
import javax.naming.Reference;
import javax.naming.Referenceable;
import javax.naming.StringRefAddr;
import javax.sql.ConnectionPoolDataSource;
import com.scxh.eei.sql.proxy.ConnectionProxy;
/**
* The <code>DataSource</code>, <code>ConnectionPoolDataSource</code> and
*
* @author
* @since
* @version $Id: DataSource.java,v 1.25 2005/02/02 13:42:49 $
*/
public class DataSource
implements javax.sql.DataSource, ConnectionPoolDataSource,
Serializable {
protected String serverName;
protected String protocolType;
protected String subname;
protected String portNumber;
protected String databaseName;
protected String url;
protected String user;
protected String password;
protected int maxsize=5,minsize=1;
protected int loginTimeout;
protected String description;
protected java.io.PrintWriter logwriter;
/**
* Driver instance used for obtaining connections.
*/
private static Driver driver;
private Hashtable poollist=new Hashtable();
/**
* Constructs a new datasource.
*/
public DataSource() {
// Do not set default property values here. Properties whose default
// values depend on server type will likely be incorrect unless the
// user specified them explicitly.
this.logwriter=new java.io.PrintWriter(System.out);
}
/**
* Returns a new database connection.
*
* @return a new database connection
* @throws SQLException if an error occurs
*/
public Connection getConnection() throws SQLException {
return getConnection(user, password);
}
/**
* Returns a new database connection for the user and password specified.
*
* @param user the user name to connect with
* @param password the password to connect with
* @return a new database connection
* @throws SQLException if an error occurs
*/
public Connection getConnection(String user, String password) throws
SQLException {
String poolname=user+":"+password;
Pool pool=(Pool) poollist.get(poolname);
PooledConnection pconn = null;
boolean needcreate=pool==null;
if (needcreate){
pool = new Pool(this.minsize,this.maxsize);
poollist.put(poolname,pool);
}
if (needcreate){
for(int i=0;i<this.minsize;i++){
pconn = (com.scxh.eei.sql.PooledConnection)this.getPooledConnection(user, password);
pconn.addConnectionEventListener(new ConnectionEventListener(pconn,
pool));
pool.unuselist.add(pconn);
}
}
if (pool.unuselist.isEmpty()) {
pconn = (com.scxh.eei.sql.PooledConnection)this.getPooledConnection(user, password);
pconn.addConnectionEventListener(new ConnectionEventListener(pconn,
pool));
synchronized(pool){
pool.usedlist.add(pconn);
return pconn.getConnection();
}
}
else {
synchronized (pool) {
pconn = (com.scxh.eei.sql.PooledConnection) pool.unuselist.firstElement();
pool.unuselist.remove(pconn);
pool.usedlist.add(pconn);
return pconn.getConnection();
}
}
}
public Driver getDriver() {
return driver;
}
public void setDriver(Driver driver) {
this.driver = driver;
}
//
// ConnectionPoolDataSource methods
//
/**
* Returns a new pooled database connection.
*
* @return a new pooled database connection
* @throws SQLException if an error occurs
*/
public javax.sql.PooledConnection getPooledConnection() throws SQLException {
return getPooledConnection(user, password);
}
/**
* Returns a new pooled database connection for the user and password specified.
*
* @param user the user name to connect with
* @param password the password to connect with
* @return a new pooled database connection
* @throws SQLException if an error occurs
*/
public synchronized javax.sql.PooledConnection getPooledConnection(String
user,
String password) throws SQLException {
if (url == null)
url = "jdbc:" + this.protocolType + ":"+this.subname+"://" + this.serverName +
(this.portNumber == null ? "" : (":" + this.portNumber))
+ "/" + this.databaseName;
Connection conn=DriverManager.getConnection(url, user, password);
if (conn==null){
this.logwriter.write("create ----conn is null");
}
PooledConnection pcon = new com.scxh.eei.sql.PooledConnection(conn);
pcon.addConnectionEventListener(null);
return pcon;
}
//
// Getters and setters
//
public PrintWriter getLogWriter() throws SQLException {
return logwriter;
}
public void setLogWriter(PrintWriter out) throws SQLException {
logwriter = out;
}
public void setLoginTimeout(int loginTimeout) throws SQLException {
this.loginTimeout = loginTimeout;
}
public int getLoginTimeout() throws SQLException {
return loginTimeout;
}
public void setMinSize(int minsize) throws SQLException {
this.minsize = minsize;
}
public int getMinSize() throws SQLException {
return minsize;
}
public void setMaxSize(int maxsize) throws SQLException {
this.maxsize = maxsize;
}
public int getMaxSize() throws SQLException {
return maxsize;
}
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
public String getDatabaseName() {
return databaseName;
}
public void setDescription(String description) {
this.description = description;
}
public String getDescription() {
return description;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
public void setPortNumber(int portNumber) {
this.portNumber = String.valueOf(portNumber);
}
public int getPortNumber() {
return Integer.parseInt(portNumber);
}
public void setServerName(String serverName) {
this.serverName = serverName;
}
public String getServerName() {
return serverName;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
public void setUser(String user) {
this.user = user;
}
public String getUser() {
return user;
}
//
public void setProtocolType(String protocolType) {
this.protocolType = protocolType;
}
public String getProtocolType() {
return protocolType;
}
public void setSubname(String subname) {
this.subname = subname;
}
public String getSubname() {
return subname;
}
}
class Pool{
Vector unuselist=new Vector(),
usedlist=new Vector();
int maxsize=100,minsize=0;
Pool(int minsize,int maxsize){
this.minsize=minsize;
this.maxsize=maxsize;
}
}
class ConnectionEventListener implements javax.sql.ConnectionEventListener{
PooledConnection pconn;
Pool pool;
ConnectionEventListener(PooledConnection pconn,Pool pool){
this.pconn=pconn;
this.pool=pool;
}
public void connectionClosed(javax.sql.ConnectionEvent event) {
synchronized(pool){
//如果池中连接数目小于最大数目,只修改连接成为可用连接,否则,关闭连接
try {
if (pool.usedlist.size() + pool.unuselist.size() <= pool.maxsize) {
//如果连接中有未提交的事务,回滚事务
if (!pconn._connection.getAutoCommit()){
pconn._connection.rollback();
}
//归还连接
pconn.freeConnection();
pool.usedlist.remove(pconn);
pool.unuselist.add(pconn);
}
else {
pool.usedlist.remove(pconn);
//真实关闭连接
pconn._connection.close();
pconn=null;
}
}
catch (SQLException e) {
pconn.fireConnectionEvent(true, e);
}
}
}
public void connectionErrorOccurred(javax.sql.ConnectionEvent event) {
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -