gpt4 book ai didi

java - 如何使用spring初始化、加载属性和处置我自己的连接池?

转载 作者:行者123 更新时间:2023-12-01 15:22:00 25 4
gpt4 key购买 nike

我有自己的连接池:

public final class ConnectionPool {


private static final Logger log = Logger.getLogger(ConnectionPool.class);

private static final int DEFAULT_POOL_SIZE = 10;


//single instance
private static ConnectionPool instance;
//queue of free connections
private BlockingQueue<Connection> connectionQueue;

public ConnectionPool(String driver, String url, String user,
String password, int poolSize)
throws ClassNotFoundException, DAOException{
try{
Class.forName(driver);
connectionQueue = new ArrayBlockingQueue<Connection>(poolSize);
for(int i = 0; i < poolSize ;i++){
Connection connection = DriverManager.getConnection(url, user, password);
connectionQueue.offer(connection);
}
}
catch (SQLException e) {
log.error(e);
throw new DAOException(e.getMessage());
}
}

public static void init() throws DAOException{
try {
if(instance == null){

String driver = ConfigurationManager.
getInstance().getProperty(ConfigurationManager.DATABASE_DRIVER_NAME);
String url = ConfigurationManager.
getInstance().getProperty(ConfigurationManager.DATABASE_URL);
String user = ConfigurationManager.
getInstance().getProperty(ConfigurationManager.DATABASE_USER);
String password = ConfigurationManager.
getInstance().getProperty(ConfigurationManager.DATABASE_PASSWORD);
String poolSizeStr = ConfigurationManager.
getInstance().getProperty(ConfigurationManager.DATABASE_POOLSIZE);
int poolSize = (poolSizeStr != null) ?
Integer.parseInt(poolSizeStr) : DEFAULT_POOL_SIZE;

log.info("Trying to create pool of connections...");

instance = new ConnectionPool(driver,url,user,password,poolSize);

log.info("Connection pool initialized");
}
}catch (ClassNotFoundException e) {
log.error(e);
} catch (SQLException e) {
log.error(e);
throw new DAOException(e.getMessage());
}
}

public static void dispose() throws DAOException {
try {
if(instance != null){
instance.clearConnectionQueue();
instance = null;
log.info("Connection queue is disposed");
}
} catch (DAOException e) {
log.info(e.getMessage());
throw new DAOException(e.getMessage());
}
}

public static ConnectionPool getInstance(){
return instance;
}

public Connection takeConnection() {
Connection connection = null;
try{
connection = connectionQueue.take();
}catch (InterruptedException e) {
log.info("Free connection waiting interrupted.Returned null connection");
log.error(e);
}
return connection;
}

public static void releaseConnection(Connection connection) throws DAOException {
try {

if(!connection.isClosed()){
if(!getInstance().connectionQueue.offer(connection)){
log.info("Connections is not added.");
}
}
else{
log.info("Trying to release closed connection.");
}
} catch (SQLException e) {
log.info("SQLException at connection isClosed(). Connection is not added");
throw new DAOException(e.getMessage());
}
}

private void clearConnectionQueue() throws DAOException{
try {
Connection connection;
while((connection = connectionQueue.poll()) != null){

if(!connection.getAutoCommit()){
connection.commit();
connection.close();
}
}
} catch (SQLException e) {
log.info(e.getMessage());
throw new DAOException(e.getMessage());
}
}



}

现在,我使用监听器初始化并销毁它,并使用我自己的类 ConfigurationManager 加载属性,并与 ResourceBundle 连接:

public final class ConfigurationManager {

private static ConfigurationManager instance;
private ResourceBundle resourceBundle;
//getting info from config.properties
private static final String BUNDLE_NAME = "config";
public static final String DATABASE_DRIVER_NAME =
"DATABASE_DRIVER_NAME";
public static final String DATABASE_URL =
"DATABASE_URL";
public static final String DATABASE_USER =
"DATABASE_USER";
public static final String DATABASE_PASSWORD =
"DATABASE_PASSWORD";
public static final String ERROR_PAGE_PATH =
"ERROR_PAGE_PATH";

public static final String BEAN_PATH =
"BEAN_PATH";
public static final String DATABASE_POOLSIZE =
"DATABASE_POOLSIZE";


public synchronized static ConfigurationManager getInstance() {
if (instance == null) {
instance = new ConfigurationManager();
instance.resourceBundle =
ResourceBundle.getBundle(BUNDLE_NAME);
}
return instance;
}
public String getProperty(String key) {
return (String)resourceBundle.getObject(key);
}
}

但我想使用 Spring 来做到这一点(初始化、销毁、属性)。那我该怎么办呢?

最佳答案

当然使用 Spring 的 init 和 destroy 方法:

http://www.mkyong.com/spring/spring-init-method-and-destroy-method-example/

我个人认为这是不明智的。您不太可能改进现有的池(例如 C3P0)。你可能会做得更糟。但这是你的选择。

关于java - 如何使用spring初始化、加载属性和处置我自己的连接池?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10760902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com