Click here to Skip to main content
15,889,858 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
package spo.db;

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

public class DBAccess
    implements Serializable

{



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?

Java
    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;
}
Posted
Updated 5-Oct-11 0:20am
v3
Comments
nazila24 2-Oct-11 23:41pm    
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?
Nagy Vilmos 5-Oct-11 6:21am    
Formatted code for readability.

1 solution

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]:

Java
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);
    }
}
 
Share this answer
 
Comments
nazila24 5-Oct-11 20:56pm    
Actually i intend to use this class to manage DBConnection Broker connection pool. i want to improve the performance of this connection pool.
The above code that you have given are about connection right?if im wrong please correct me...i'm try to modify my code to add log detail method in the class.
Nagy Vilmos 6-Oct-11 3:39am    
Yes this example is the 'outside' bit. The design should be that the consumer application gets an access object, and it doesn't matter if there is a connection per access instance or if they are pooled.
The issue you will encounter is 'greedy' coding on the client side. If the client has to ak for a connection, it is unlikely that they'll give it back in a timely manner, that's just the nature of the beast. So don't allow the client to connect to the db layer only the access layer which encapsulates the pooling so that it is managed in one place.

Remember, who is the expert about the connection? The client that submits statements or the access layer that gets and returns connections?
nazila24 5-Oct-11 23:25pm    
Can you help me to explain about the above class.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900