好文档 - 专业文书写作范文服务资料分享网站

关于ConnectionPooljar数据连接池文件包的制作流程

天下 分享 时间: 加入收藏 我要投稿 点赞

************************文件开始***************************

db.properties //属性文件

***********************************************************

XBDBManager Properties

poolname = sqlpool oraclepool 注:中间以空格分隔

drivers = net.sourceforge.jtds.jdbc.Driver oracle.jdbc.driver.OracleDriver 注:中间以空格分隔

logfile = d:\\\\logfile.txt

sqlpool.url = jdbc:jtds:sqlserver://10.1.1.2:1433;DatabaseName=pda sqlpool.initconns=50 sqlpool.maxconns=80 sqlpool.user = pda

sqlpool.password = pdapass1234

oraclepool.url = jdbc:oracle:thin:@172.168.72.8:1521:infodb oraclepool.initconns=50 oraclepool.maxconns=80 oraclepool.user = tjold

oraclepool.password = tjold

************************文件结束***************************

************************文件开始***************************

ConnectionPool.java

***********************************************************

package ConnectionPool;

import java.io.*; import java.sql.*; import java.util.*;

public class ConnectionPool { private String name; private String URL; private String user; private String password; private int maxConns; private int timeOut;

private LogWriter logWriter;

private int checkedOut;

private Vector freeConnections = new Vector();

public ConnectionPool(String name, String URL, String user,

String password, int maxConns, int initConns, int timeOut, PrintWriter pw, int logLevel) {

this.name = name; this.URL = URL; this.user = user;

this.password = password; this.maxConns = maxConns;

this.timeOut = timeOut > 0 ? timeOut : 5;

logWriter = new LogWriter(name, logLevel, pw); initPool(initConns);

logWriter.log(\ String lf = System.getProperty(\

logWriter.log(

\ \

// \ \ \

\ logWriter.log(getStats(), LogWriter.INFO); }

private void initPool(int initConns) {

for (int i = 0; i < initConns; i++) { try {

Connection pc = newConnection(); freeConnections.addElement(pc); }

catch (SQLException e) { } } }

public Connection getConnection() throws SQLException {

logWriter.log(\for connection received\LogWriter.DEBUG); try {

Connection conn = getConnection(timeOut * 1000); return new ConnectionWrapper(conn, this); }

catch(SQLException e) {

logWriter.log(e, \getting connection\logWriter.ERROR);

throw e; } }

synchronized void wrapperClosed(Connection conn) {

freeConnections.addElement(conn); checkedOut--; notifyAll();

logWriter.log(\pool\ logWriter.log(getStats(), LogWriter.INFO); }

private synchronized Connection getConnection(long timeout) throws SQLException {

// Get a pooled Connection from the cache or a new one. // Wait if all are checked out and the max limit has // been reached.

long startTime = System.currentTimeMillis(); long remaining = timeout; Connection conn = null;

while ((conn = getPooledConnection()) == null) { try {

logWriter.log(\for connection. Timeout=\+ remaining,

LogWriter.DEBUG); wait(remaining); }

catch (InterruptedException e) { }

remaining = timeout - (System.currentTimeMillis() - startTime);

if (remaining <= 0) {

// Timeout has expired

logWriter.log(\while waiting for connection\

LogWriter.DEBUG); throw new SQLException(\timed-out\

} }

// Check if the Connection is still OK if (!isConnectionOK(conn)) {

// It was bad. Try again with the remaining timeout

logWriter.log(\selected bad connection from pool\

LogWriter.ERROR);

return getConnection(remaining); }

checkedOut++;

logWriter.log(\getConnection from pool\LogWriter.INFO);

logWriter.log(getStats(), LogWriter.INFO); return conn; }

private boolean isConnectionOK(Connection conn) { Statement testStmt = null; try {

if (!conn.isClosed()) {

// Try to createStatement to see if it's really alive

testStmt = conn.createStatement(); testStmt.close(); }

else {

return false; } }

catch (SQLException e) {

if (testStmt != null) { try {

testStmt.close(); }

catch (SQLException se) { } }

logWriter.log(e, \Connection was not okay\LogWriter.ERROR);

return false; }

return true; }

private Connection getPooledConnection() throws SQLException { Connection conn = null;

if (freeConnections.size() > 0) {

// Pick the first Connection in the Vector // to get round-robin usage

conn = (Connection) freeConnections.firstElement(); freeConnections.removeElementAt(0); }

else if (maxConns == 0 || checkedOut < maxConns) { conn = newConnection(); }

return conn; }

private Connection newConnection() throws SQLException {

关于ConnectionPooljar数据连接池文件包的制作流程

************************文件开始***************************db.properties//属性文件***********************************************************XBDBManagerPropertiespoolnam
推荐度:
点击下载文档文档为doc格式
09y6e40ouw81m9s40mcz3j4le87mw200j80
领取福利

微信扫码领取福利

微信扫码分享