关闭REST服务中的连接池是否会首先破坏连接池的用途?

关闭REST服务中的连接池是否会首先破坏连接池的用途?,rest,jdbc,bonecp,Rest,Jdbc,Bonecp,我使用BoneCP将jdbc连接池连接到mysql数据库。我正在REST应用程序中使用bonecp示例 如果每一个REST请求都在打开和关闭连接池,那么这不是首先破坏了连接池的作用吗 代码如下: public class ExampleJDBC { /** Start test * @param args none expected. */ public static void main(String[] args) { BoneCP connectionPool = null;

我使用BoneCP将jdbc连接池连接到mysql数据库。我正在REST应用程序中使用bonecp示例

如果每一个REST请求都在打开和关闭连接池,那么这不是首先破坏了连接池的作用吗

代码如下:

 public class ExampleJDBC {

/** Start test
 * @param args none expected.
 */
public static void main(String[] args) {
    BoneCP connectionPool = null;
    Connection connection = null;

    try {
        // load the database driver (make sure this is in your classpath!)
        Class.forName("org.hsqldb.jdbcDriver");
    } catch (Exception e) {
        e.printStackTrace();
        return;
    }

    try {
        // setup the connection pool
        BoneCPConfig config = new BoneCPConfig();
        config.setJdbcUrl("jdbc:hsqldb:mem:test"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb
        config.setUsername("sa"); 
        config.setPassword("");
        config.setMinConnectionsPerPartition(5);
        config.setMaxConnectionsPerPartition(10);
        config.setPartitionCount(1);
        connectionPool = new BoneCP(config); // setup the connection pool

        connection = connectionPool.getConnection(); // fetch a connection

        if (connection != null){
            System.out.println("Connection successful!");
            Statement stmt = connection.createStatement();
            ResultSet rs = stmt.executeQuery("SELECT 1 FROM      INFORMATION_SCHEMA.SYSTEM_USERS"); // do something with the connection.
            while(rs.next()){
                System.out.println(rs.getString(1)); // should print out "1"'
            }
        }
        connectionPool.shutdown(); // shutdown connection pool.
    } catch (SQLException e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
} 
 }

是的,在应用程序的(典型)生命周期中多次打开或关闭连接池将无法实现。每次都应该从预先建立的池中获取连接