📄 connectionadaptorpool.java
字号:
package org.speedframework.connection;
//~--- non-JDK imports --------------------------------------------------------
import org.apache.log4j.Logger;
import org.speedframework.config.SpeedConfig;
import org.speedframework.exception.SpeedFrameworkException;
import org.speedframework.utilities.ConfigureParam;
//~--- JDK imports ------------------------------------------------------------
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.*;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
/**
* 类描述信息,描述类的主要职责和用处。
*
*
* @version $LastChangedRevision: 1945 $, 2007.09.29 at 02:14:26 CST
* @author <a href="mailto:falcon8848@gmail.com">piginzoo </a>
*/
public class ConnectionAdaptorPool
{
/** 属性描述信息 */
private static int connectionCount = 0;
/** 属性描述信息 */
private static final Logger log = Logger.getLogger(ConnectionAdaptorPool.class);
/** 属性描述信息 */
private static Map adaptorPoolMap = new HashMap();
/** 属性描述信息 */
private int maxConnectionCount = 10;
/** 属性描述信息 */
private long maxWaitTime = 3000;
/** 属性描述信息 */
private HashSet pool = new HashSet();
/** 属性描述信息 */
private String connectionFactoryId;
/**
* Constructs ...
*
*
* @param connectionFactoryId
*/
private ConnectionAdaptorPool(String connectionFactoryId) {
this.connectionFactoryId = connectionFactoryId;
this.maxConnectionCount = SpeedConfig.getMaxConnectionCountById(connectionFactoryId);
this.maxWaitTime = SpeedConfig.getMaxWaitTimeById(connectionFactoryId);
}
/**
* 方法描述信息,
* 描述方法是做什么的,
* 如何调用,最好给出调用代码示例。
*
* @return
*
* @throws SpeedFrameworkException
*/
public synchronized IConnectionAdaptor getConnectionAdapter() throws SpeedFrameworkException {
// ???l????????????
if (connectionCount >= maxConnectionCount) {
// System.out.println(Thread.currentThread().getName()+" waiting...");
try {
wait(maxWaitTime);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
throw new SpeedFrameworkException(e);
}
}
// l???????????
if (connectionCount >= maxConnectionCount) {
throw new SpeedFrameworkException("Connection Time Out!");
}
// l???锟斤拷????????l??
Iterator it = pool.iterator();
SpeedConnectionAdaptor adaptor;
while (it.hasNext()) {
adaptor = (SpeedConnectionAdaptor) it.next();
if (!adaptor.getConnectionProxy().isInUse()) {
adaptor.getConnectionProxy().setInUse(true);
log.debug(Thread.currentThread().getName() + " get a free connection. connectionCount="
+ (++connectionCount));
return adaptor;
}
}
log.debug(Thread.currentThread().getName() + " creating a new adaptor");
// 锟斤拷???????????????????l????????
SpeedConnectionAdaptor newAdaptor = new SpeedConnectionAdaptor();
ConnectionProxy connProxy = new ConnectionProxy(getConnection(connectionFactoryId), true,
connectionFactoryId);
newAdaptor.setConnectionProxy(connProxy);
pool.add(newAdaptor);
connectionCount++;
return newAdaptor;
}
/**
* 方法描述信息,
* 描述方法是做什么的,
* 如何调用,最好给出调用代码示例。
*
* @param id
*
* @return
*/
public static ConnectionAdaptorPool getPoolById(String id) {
ConnectionAdaptorPool adaptor = (ConnectionAdaptorPool) adaptorPoolMap.get(id);
if (adaptor == null) {
adaptor = new ConnectionAdaptorPool(id);
}
adaptorPoolMap.put(id, adaptor);
return adaptor;
}
/**
* 方法描述信息,
* 描述方法是做什么的,
* 如何调用,最好给出调用代码示例。
*/
public synchronized void releaseAdaptor() {
connectionCount--;
notify();
}
/**
* 方法描述信息,
* 描述方法是做什么的,
* 如何调用,最好给出调用代码示例。
*
* @param id
*
* @return
*
* @throws SpeedFrameworkException
*/
private Connection getConnection(String id) throws SpeedFrameworkException {
Map connectionFactory = new HashMap();
String type = "";
Connection conn = null;
if ((id != null) && (id.length() > 0)) {
connectionFactory = SpeedConfig.getConnectionFactoryById(id);
type = SpeedConfig.getConnectionType(id);
} else {
connectionFactory = SpeedConfig.getDefaultConnectionFactory();
type = SpeedConfig.getConnectionType();
}
if (type.equals("connection")) {
String className = null;
String url = null;
String userName = null;
String password = null;
className = (String) connectionFactory.get(ConfigureParam.DRIVER);
url = (String) connectionFactory.get(ConfigureParam.URL);
userName = (String) connectionFactory.get(ConfigureParam.USERNAME);
password = (String) connectionFactory.get(ConfigureParam.PASSWORD);
try {
Class.forName(className);
conn = DriverManager.getConnection(url, userName, password);
} catch (Exception e) {
throw new SpeedFrameworkException(e);
}
}
if (type.equals("datasource")) {
String jndiName = null;
String jndiUserName = null;
String jndiPassword = null;
String jndiClass = null;
String jndiUrl = null;
String initialContextFactory = null;
String providerUrl = null;
jndiName = (String) connectionFactory.get(ConfigureParam.JNDI_NAME);
jndiUserName = (String) connectionFactory.get(ConfigureParam.JNDI_USERNAME);
jndiPassword = (String) connectionFactory.get(ConfigureParam.JNDI_PASSWORD);
jndiClass = (String) connectionFactory.get(ConfigureParam.JNDI_CLASS);
jndiUrl = (String) connectionFactory.get(ConfigureParam.JNDI_URL);
initialContextFactory = (String) connectionFactory.get(ConfigureParam.INITIAL_CONTEXT_FACTORY);
providerUrl = (String) connectionFactory.get(ConfigureParam.PROVIDER_URL);
Properties prop = null;
Context context = null;
try {
if ((initialContextFactory != null) && (providerUrl != null)) {
prop = new Properties();
prop.put(Context.INITIAL_CONTEXT_FACTORY, initialContextFactory);
prop.put(Context.PROVIDER_URL, providerUrl);
context = new InitialContext(prop);
conn = ((DataSource) context.lookup(jndiName)).getConnection();
} else {
context = new InitialContext();
if ((jndiUserName != null) && (jndiPassword != null)) {
conn = ((DataSource) context.lookup("java:comp/env/" + jndiName)).getConnection(jndiUserName,
jndiPassword);
} else {
conn = ((DataSource) context.lookup("java:comp/env/" + jndiName)).getConnection();
}
}
} catch (Exception e) {
new SpeedFrameworkException(e);
}
}
return conn;
}
/**
* 方法描述信息,
* 描述方法是做什么的,
* 如何调用,最好给出调用代码示例。
*/
protected void finalize() {
Iterator it = pool.iterator();
ConnectionProxy connProxy;
SpeedConnectionAdaptor adaptor;
while (it.hasNext()) {
adaptor = (SpeedConnectionAdaptor) it.next();
connProxy = (ConnectionProxy) (adaptor.take());
try {
connProxy.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -