Oracle JDBC Pool Connection访问分配策略?

时间:2012-01-21 19:25:22

标签: oracle jdbc connection-pooling

使用Oracle JDBC连接池时,有没有办法控制连接的分配方式?特别是,有没有办法指定使用LIFO策略?似乎这种联系可能以循环方式发布。

在这种情况下:

  • 使用池中的最大连接数(10)
  • 此后,只使用了一个并发连接,每5秒检索并返回
  • 不活动超时设置为60秒

如果使用循环策略,则将在60秒的时间段内使用10个池化连接中的每一个。当发生非活动超时检查时,每个连接都将在最后一分钟内处于活动状态,因此没有连接将成为要关闭的候选者。连接池将保留10个连接,但实际上只需要1个连接。至少这是我似乎正在经历的事情。我希望游泳池缩小到只有一个连接。

我对驱动程序的工作原理有何了解?有没有办法从池中控制连接分配策略(LIFO,FIFO,循环)或者我是否必须使用其他池化机制?

以下是测试(使用弃用的apis)。在这种情况下,创建了3个连接,它只会缩减到2而不是1。

编辑以更密切地反映以上描述:

import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import oracle.jdbc.pool.OracleDataSource;


public class Main {

    public static void main(String[] args) throws InterruptedException, IOException, ClassNotFoundException, SQLException {

        String url = "jdbc:oracle:thin:@//host:1521/SID";
        String user = "user";
        String pwd = "pwd";

        OracleDataSource ocpds;

        ArrayList<Connection> tempConnList = new ArrayList<>();


        try {

            ocpds = new OracleDataSource();
            ocpds.setURL(url);
            ocpds.setUser(user);
            ocpds.setPassword(pwd);

            java.util.Properties prop = new java.util.Properties();
            prop.setProperty("MinLimit", "1");
            prop.setProperty("MaxLimit", "10");

            prop.setProperty("InactivityTimeout", "60");    //  seconds
            prop.setProperty("AbandonedConnectionTimeout", "60");  //  seconds
            prop.setProperty("PropertyCheckInterval", "60"); // seconds            

            // set DataSource properties
            ocpds.setConnectionCachingEnabled(true);
            ocpds.setConnectionCacheProperties(prop);
            ocpds.setConnectionCacheName("TestCache");


            // Ramp up to max
            for (int i=0; i<10; i++) {
                Connection conn = ocpds.getConnection();
                tempConnList.add(conn);
            }

            // Release them all
            for (Connection conn : tempConnList) {
                conn.close();
            }


            // Grab and release one connection at a time
            for (int i = 0; i < 60; i++) {

                System.out.println(new java.util.Date());

                // Grab and release
                Connection conn = ocpds.getConnection();
                conn.close();

                try {
                    Thread.currentThread().sleep(5000);
                } catch (InterruptedException ie) {
                    System.err.println("error message: " + ie.getMessage());
                }

            }

        } catch (SQLException e) {
            System.err.println("error message: " + e.getMessage());
        } finally {
            for (Connection conn : tempConnList) {
                if (conn != null) { try { conn.close(); } catch (SQLException ignored) {}; }
            }
        }
    }

}

1 个答案:

答案 0 :(得分:2)

Oracle支持部门已回复说,循环方法用于反馈池中的连接。在Oracle JDBC 12(当前版本为11.2.0.3)中,将有一个属性“UseLIFO”,它将允许“后进先出”检索:

prop.setProperty("UseLIFO", "true");

在问题中发布的示例中,这将允许空闲时间将池缩小为单个连接。

相关问题