增强连接池 [英] Enhancement of Connection Pooling

查看:76
本文介绍了增强连接池的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

package spo.db;

import com.javaexchange.dbConnectionBroker.DbConnectionBroker;
import java.io.*;
import java.sql.*;
import java.util.Properties;

public class DBAccess
    implements Serializable

{




上面的代码是DBAcess.java.我试图修改这些代码以提高连接池的性能.修改示例包括添加详细日志,添加连接标题,了解数据库的来源.如何进行修改?




The above code are DBAcess.java. im trying to make modification of those code to improve performance of connection pool. Example of modification are add the detail log, add connection heading, know source of database. How to make those modification?

    public DBAccess()
        throws SQLException
    {
        try
        {
            prop = new Properties();
            prop.load(new FileInputStream("spo.properties"));
            String s = prop.getProperty("db.driver");
            String s1 = prop.getProperty("db.url");
            String s2 = prop.getProperty("db.username");
            String s3 = prop.getProperty("db.password");
            int i = Integer.parseInt(prop.getProperty("db.min"));
            int j = Integer.parseInt(prop.getProperty("db.max"));
            String s4 = prop.getProperty("db.log");
            double d = 0.01D;
            myBroker = new DbConnectionBroker(s, s1, s2, s3, i, j, s4, d);
        }
        catch(IOException ioexception)
        {
            ioexception.printStackTrace();
            System.err.println((new StringBuilder()).append("connection error").append(ioexception).toString());
            System.exit(-1);
        }
    }

    public static Connection getDBConnection()
        throws SQLException
    {
        Connection connection1 = myBroker.getConnection();
        if(connection1 == null)
            throw new SQLException("Cannot allocate connection from database connection pool.");
        else
            return connection1;
    }

    public static void returnDBConnection(Connection connection1)
    {
        myBroker.freeConnection(connection1);
    }

    public static String returnID(Connection connection1)
    {
        int i = myBroker.idOfConnection(connection1);
        return (new StringBuilder()).append(
                "<h3>DbConnectionBroker</h3>Using connection "
                ).append(i).append(" from connection pool<p>").toString();
    }

    public static synchronized int getSeqID(Connection connection1, String s)
        throws SQLException
    {
        int i = Integer.parseInt(prop.getProperty("db.code"));
        String s1 = prop.getProperty("db.seq");
        if(i == 1)
            try
            {
                File file = new File((new StringBuilder()).append(s1).append(s).append(".dat").toString());
                BufferedReader bufferedreader = new BufferedReader(new FileReader(file));
                int j = Integer.parseInt(bufferedreader.readLine());
                j++;
                bufferedreader.close();
                PrintWriter printwriter = new PrintWriter(new FileOutputStream(file));
                printwriter.println(j);
                printwriter.flush();
                printwriter.close();
                return j;
            }
            catch(Exception exception)
            {
                throw new SQLException((
                        new StringBuilder()).append(
                                exception.getMessage()).append(": Cannot get ID from ").append(s).toString());
            }
        String s2 = (new StringBuilder()).append("select ").append(s).append(".nextval from dual").toString();
        Statement statement1 = connection1.createStatement();
        ResultSet resultset = statement1.executeQuery(s2);
        if(!resultset.next())
        {
            throw new SQLException((
                    new StringBuilder()).append("Error when tring to execute: ").append(s2).toString());
        } else
        {
            int k = resultset.getInt(1);
            resultset.close();
            statement1.close();
            return k;
        }
    }

    private static DbConnectionBroker myBroker;
    private Connection connection;
    private Statement statement;
    protected static Properties prop;
}

推荐答案

我不会公开DbAccess 类的真实连接.
该类具有对代理的引用,因此不需要保留线程.我不知道您打算如何使用该类,但由于它是您,即使没有使用它,您也将保持数据库连接.
在访问类中,实现对连接的访问​​.如果没有连接,则每次接收到命令时,都会从代理获取连接.如果启动了事务,则连接将保持到提交或回滚为止.快速播放示例[未构建]:

I would not expose the real connection from the DbAccess class.
The class has a reference to the broker, so it does not need to hold on to the thread. I don''t know how you intend to use the class, but as it is you will hold the database connection even if it is not in use.
In the access class, implement access to the connection. If there is no connection, then each time a command is received a connection is taken from the broker. If a transaction is started, the connection is kept until it is committed or rolled back. A quick play example [not built]:

public class DbAccess {
    private final static DbConnectionBroker broker;
    private Connection connection;
    private int commitCount;
    // ... other stuff ignored
    
    public Static() {
        // set up ONCE for every instance
        // ... config ...
        DbAccess.broker = new DbConnectionBroker(...);
    }
    
    public DbAccess() {
        // initial state:
        this.commitCount = 0;
    }
    
    // note this is private.
    private Connection getConnection() {
        if (this.connection == null) {
            // returns a temporary connection
            // each time it is called:
            return DbAccess.getConnection();
        }
        return this.connection;
    }
    
    public void beginTransaction() {
        // incriment commit count and get a permenant connection
        this.commitCount++;
        if (this.connection == null) {
            this.connection = this.getConnection();
            this.connection.beginTransaction();
        }
    }
    
    public void commit() {
            throws InvalidCommitState {
        if (this.connection == null) {
            // some error
            throw new InvalidCommitState();
        }
        // decrement commit count and release permenant connection if we're at zero
        this.commitCount--;
        if (this.commitCount == 0} {
            this.connection.commit();
            this.broker.releaseConnection(this.connection);
            this.connection = null;
        }
    }

    public void rollback() {
            throws InvalidCommitState {
        if (this.connection == null) {
            // some error
            throw new InvalidCommitState();
        }
        // decrement commit count and release permenant connection if we're at zero
        this.commitCount = 0;
        this.connection.rollback();
        this.broker.releaseConnection(this.connection);
        this.connection = null;
    }
    
    public void execute (Statement statement) {
        this.getConnection().execute(statement);
    }
}


这篇关于增强连接池的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆