English 中文(简体)
如何使用春天来启动、加载属性和处置我自己的连接集合?
原标题:How to initialise,load properties and dispose my own connection pool using spring?

我有自己的连接池:

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());
    }
}



}

使用我自己的类 < code> ConfigisationManager , 与 < code> ResourcesBundle 连接, 并用收听器和装入属性来销毁它:

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);
}
 }

但我想用春天来做它(尼特,破坏,财产),我该怎么做呢?

最佳回答

使用弹簧喷射和销毁方法,当然包括:

http://www.mkyong.com/spring/spring-inring-init-method-and-destroy-method-example/"rel="nofollow">http://www.mkyong.com/spring/spring-init-method-and-destroy-method-example/

就个人而言,我认为这是不明智的。你不可能改善已经拥有的游泳池(例如C3P0),你可能会做得更糟。但这是你的选择。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签