Java poolDataSource.getConnection()挂起。尝试使用ojdbc6和ojdbc8 JAR
我正在使用Oracle11gXe和Java8。我正在开发一个桌面应用程序,想做连接池。应用程序在执行pds.getConnection()时挂起;我尝试使用简单的JDBC,它工作得很好,所以它不是网络/DB问题。这是我打电话的密码-Java poolDataSource.getConnection()挂起。尝试使用ojdbc6和ojdbc8 JAR,java,jdbc,oracle11g,connection-pooling,Java,Jdbc,Oracle11g,Connection Pooling,我正在使用Oracle11gXe和Java8。我正在开发一个桌面应用程序,想做连接池。应用程序在执行pds.getConnection()时挂起;我尝试使用简单的JDBC,它工作得很好,所以它不是网络/DB问题。这是我打电话的密码- /** * This returns DB connection from the DB Connection Pool * * @return * @throws IOException * @throws SQLException */ publi
/**
* This returns DB connection from the DB Connection Pool
*
* @return
* @throws IOException
* @throws SQLException
*/
public static Connection getConnection() throws IOException, SQLException {
if (pds == null) {
initConnectionPool();
}
Connection conn = pds.getConnection();
System.out.println("Available connections after checkout: " + pds.getAvailableConnectionsCount());
System.out.println("Borrowed connections after checkout: " + pds.getBorrowedConnectionsCount());
return conn;
}
/**
* init DB Connection Pool
*
* @return
* @throws IOException
* @throws SQLException
*/
private static void initConnectionPool() throws IOException, SQLException {
Properties configProperties = Utils.getConfig();
// Get the PoolDataSource for UCP
PoolDataSource pds = PoolDataSourceFactory.getPoolDataSource();
// Set the connection factory first before all other properties
System.out.println("Class name :" + configProperties.getProperty("CONNECTION_FACTORY_CLASS_NAME"));
pds.setConnectionFactoryClassName(configProperties.getProperty("CONNECTION_FACTORY_CLASS_NAME"));
System.out.println("DB URL ==" + configProperties.getProperty("DB_URL"));
pds.setURL(configProperties.getProperty("DB_URL"));
System.out.println("DB User ==" + configProperties.getProperty("DB_USER"));
pds.setUser(configProperties.getProperty("DB_USER"));
System.out.println("Password==" + configProperties.getProperty("DB_PSWD"));
pds.setPassword(configProperties.getProperty("DB_PSWD"));
pds.setConnectionPoolName("JDBC_UCP_POOL");
// Default is 0. Set the initial number of connections to be created
// when UCP is started.
pds.setInitialPoolSize(Integer.parseInt(configProperties.getProperty("INITIAL_POOL_SIZE")));
// Default is 0. Set the minimum number of connections
// that is maintained by UCP at runtime.
pds.setMinPoolSize(Integer.parseInt(configProperties.getProperty("MIN_POOL_SIZE")));
// Default is Integer.MAX_VALUE (2147483647). Set the maximum number of
// connections allowed on the connection pool.
pds.setMaxPoolSize(Integer.parseInt(configProperties.getProperty("MAX_POOL_SIZE")));
}
/**
* Loads the config file and return instance of Properties
*
* @return Proterties
* @throws IOException
*/
public static Properties getConfig() throws IOException {
if (configProps != null)
return configProps;
configProps = new Properties();
FileInputStream in;
in = new FileInputStream("bin/resources/config.properties");
configProps.load(in);
in.close();
return configProps;
}
使用的属性是-
DB_URL=jdbc:oracle:thin:@//localhost:1521/XE
初始池大小=5
最小池大小=5
最大池大小=10
CONNECTION\u FACTORY\u CLASS\u NAME=oracle.jdbc.pool.OracleDataSource您没有初始化
pds
类变量。调用getConnection()
时,它总是null
为了确保初始化池的方法在使用静态初始化程序块后被调用
静止的{
initConnectionPool();
}我发现了错误。在initialize()中,我声明了一个局部变量pds。我删除了它,让类变量得到初始化,代码开始工作。我很惊讶为什么在这种情况下我没有得到NullPointerException。。这本可以节省很多时间。我已经将pds声明为类级静态变量。如果发现pds为null,我将调用initConnectionPool()。我没有';不要要求您声明一个类变量,因为它不会';否则我不会编译。你没有,#39;不要初始化那个变量。变量应该在上下文启动后初始化,并且只初始化一次。如果变量是静态的,则可以在加载类时对其进行初始化。