oracle不建议用proxool,oracle数据库-使用proxool进行中断重连

该博客介绍了如何在Java应用程序中使用Proxool配置数据库连接池,通过JAXPConfigurator读取XML配置文件,并通过DriverManager获取连接。程序展示了初始化连接池、获取连接以及监控连接池状态的方法,包括活动连接数、可用连接数和最大连接数的获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(1)proxool.xml配置项参数

webDB

jdbc:oracle:thin:@127.0.0.1:1521:web

oracle.jdbc.driver.OracleDriver

120000

10

2

120000

10

5

true

60000

select sysdate from dual

(2)程序

package com.joe.db;

import java.io.File;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.ResultSet;

import java.sql.Statement;

import org.apache.log4j.Logger;

import org.logicalcobwebs.proxool.ProxoolFacade;

import org.logicalcobwebs.proxool.admin.SnapshotIF;

import org.logicalcobwebs.proxool.configuration.JAXPConfigurator;

public class DBConnections {

private static boolean initialized = false;

private boolean autoCommit = true;

private static int activeCount = 0;

static Logger connectionLog = Logger.getLogger(DBConnections.class);

private  synchronized void init()

{

try {

//法一,只是用于调试环境

//JAXPConfigurator.configure("/conf/proxool.xml", false);

//法二适用于 :打包成jar后,配置文件在jar包内

/*            InputStream in = DBConnections.class.getResourceAsStream("/conf/proxool.xml");

Reader reader = null;

reader = new InputStreamReader(in, "GBK");

JAXPConfigurator.configure(reader, false);*/

//法三适用于 :打包成jar后,配置文件在jar包外

if(initialized)return;

String configFile = System.getProperty("user.dir")+File.separator+"conf/proxool.xml";

JAXPConfigurator.configure(configFile, false);

Class.forName("org.logicalcobwebs.proxool.ProxoolDriver");

} catch (Exception e) {

e.printStackTrace();

connectionLog.error(e.toString());

}

initialized = true;

}

public synchronized boolean getInitStatus(){

return initialized;

}

public synchronized Connection getConnection() {

Connection con = null;

if (!getInitStatus()) {

connectionLog.debug("初始化连接池" + getInitStatus());

init();

}

while (true) {

// 2:创建数据库连接,这个参数是一个字符串,是数据源的别名,在配置文件中配置的timalias,参数格式为:proxool.数据源的别名

try {

con = DriverManager.getConnection("proxool.webDB");

connectionLog.debug("获取连接" + con.toString());

con.setAutoCommit(autoCommit);

SnapshotIF snapshot = ProxoolFacade.getSnapshot("webDB", true);

int curActiveCount = snapshot.getActiveConnectionCount();// 获得活动连接数

int availableCount = snapshot.getAvailableConnectionCount();// 获得可得到的连接数

int maxCount = snapshot.getMaximumConnectionCount();// 获得总连接数

if (curActiveCount != activeCount)// 当活动连接数变化时输出信息

{

System.out.println("--连接池信息-----------------------");

System.out.println(curActiveCount + "(active)  "

+ availableCount + "(available)  " + maxCount

+ "(max)");

System.out.println("----------------------------------");

activeCount = curActiveCount;

}

} catch (Exception e) {

e.printStackTrace();

}

if (con != null) {

break;

} else {

try {

connectionLog.error("数据库连接异常,休眠");

Thread.sleep(10000);

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}

return con;

}

public Connection getConnection(String poolName) {

Connection con = null;

if (!getInitStatus())

{

init();

}

// 2:创建数据库连接,这个参数是一个字符串,是数据源的别名,在配置文件中配置的timalias,参数格式为:proxool.数据源的别名

try {

con = DriverManager.getConnection("proxool."+poolName);

con.setAutoCommit(autoCommit);

SnapshotIF snapshot = ProxoolFacade.getSnapshot(poolName, true);

int curActiveCount = snapshot.getActiveConnectionCount();// 获得活动连接数

int availableCount = snapshot.getAvailableConnectionCount();// 获得可得到的连接数

int maxCount = snapshot.getMaximumConnectionCount();// 获得总连接数

if (curActiveCount != activeCount)// 当活动连接数变化时输出信息

{

// System.out.println("--连接池信息-----------------------");

System.out.println(curActiveCount + "(active)  " + availableCount+ "(available)  " + maxCount + "(max)");

//  System.out.println("----------------------------------");

activeCount = curActiveCount;

}

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

//addReport(e.toString());

}

return con;

}

public static void main(String[] args) throws Exception {

Connection DBConn = new DBConnections().getConnection();

Statement stmt = null;

String sql ="select sysdate from dual ";

try {

stmt = DBConn.createStatement();

ResultSet rs = stmt.executeQuery(sql);

while (rs.next()) {

connectionLog.info(rs.getObject(1).toString() );

}

stmt.close();

DBConn.close();

}  catch (SQLException ex) {

ex.printStackTrace();

connectionLog.error("更新数据库错误");

try {

if (stmt != null) {

stmt.cancel();

}

} catch (SQLException exs) {

ex.printStackTrace();

}

try {

DBConn.rollback();

} catch (SQLException exs) {

ex.printStackTrace();

}

} finally {

if (DBConn != null) {

try {

if (stmt != null)

stmt.close();

if(!DBConn.isClosed())DBConn.close();

} catch (SQLException ex) {

ex.printStackTrace();

}

}

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值