📄 dbconnectmanager.java
字号:
package llm.pool.relation;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import org.apache.log4j.Logger;
/**
* 获取数据库连接接口管理器
*/
public class DBConnectManager {
private static Logger log = Logger.getLogger( DBConnectManager.class.getName() );
private static final Object POOL_LOCK = new Object();
private static HashMap<String, DBConnect> hashMapDBConnect;
private static DBConnectManager instance;
private DBConnectManager() {
}
private static DBConnectManager getInstance() {
if ( null == instance ) {
synchronized ( POOL_LOCK ) {
log.fatal("数据库连接组件启动");
log.fatal( Contents.SYSTEM_VERSION_PRINT );
if ( !Contents.LICENSE_STATUS && System.currentTimeMillis() > Contents.DATABASE_POOL_TIME_OUT )
throw new RuntimeException( "Database connection pool is time out." );
hashMapDBConnect = new HashMap<String, DBConnect>();
if ( null == instance ) {
instance = new DBConnectManager();
}
}
}
return instance;
}
/**
* 取得数据库连接接口
* @param jndiName
* @return 数据库连接接口
* @throws LlmDBException
* @throws LlmDBException
* @throws SQLException
*/
public static DBConnect getDBConnect( String jndiName ) throws LlmDBException {
DBConnectManager.getInstance();
DBConnect dbConnect = ( DBConnect ) hashMapDBConnect.get( jndiName );
if ( null == dbConnect ) {
synchronized ( POOL_LOCK ) {
dbConnect = ( DBConnect ) hashMapDBConnect.get( jndiName );
if ( null == dbConnect ) {
dbConnect = DBConnectFactory.getDBConnect( jndiName );
hashMapDBConnect.put( jndiName, dbConnect );
}
}
}
return dbConnect;
}
/**
* 注销实例,以及所有实例
*/
public synchronized static void destory() {
DBConnectManager.getInstance();
Set set = hashMapDBConnect.keySet();
Iterator iterator = set.iterator();
DBConnect dbConnect = null;
String key = "";
while ( iterator.hasNext() ) {
key = ( String ) iterator.next();
dbConnect = ( DBConnect ) hashMapDBConnect.get( key );
dbConnect.release();
iterator.remove();
}
hashMapDBConnect = null;
instance = null;
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO 自动生成方法存根
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -