• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 275
  • Last Modified:

My Connection Pooling are not Persistant. It closes in at a several time. In Pure Java.

Hi All,

I have written my own connection pool. However, I run the application for one day, it closes automatically. I just wonder that is it a problem in my coding or in my database or tomcat setting.
Thank you so much.

I am using MYSQL and Java.

    public class DBConnection extends DataConnection {
        public DBConnection(WebSite website)
        throws Exception {
            open(website.databaseDriver,website.databaseUrl,website.databaseUsername,website.databasePassword);
        }
    }
   
    public abstract class DataConnection {
        public Connection innerConnection;
       
        public boolean open(String driver,String url,String username,String password) throws Exception {
            Class.forName(driver);
            innerConnection = DriverManager.getConnection(url,username,password);
            if(innerConnection==null)
                return false;
            return true;
        }
       
        public void close() {
            try {
                innerConnection.close();
                System.out.println("Database Connection Closed Successfully..");
            } catch(Exception e) {
            }
        }
       
        public PreparedStatement prepareStatement(String sql) throws SQLException {
            return innerConnection.prepareStatement(sql);
        }
       
        protected void finalize() throws Throwable {
            close();
            super.finalize();
        }
    }
    void loadDatabaseConfiguration()
    //throws WebSiteError
    throws Exception {
        try {
            XMLParser parser = new XMLParser();
            if(!parser.parseFile( getConfPath() + java.io.File.separator + "database.xml" )) {
                throw new WebSiteError("Database information missing, Please setup database ","Click here to setup database","/conf/setup/");
            }
           
            databasexml = parser.parseFileXML( getConfPath() + java.io.File.separator + "database.xml" );
            databaseDriver = parser.getValue("database/driver");
            databaseUrl = parser.getValue("database/url");
            //      databaseDriver = "com.mysql.jdbc.Driver";
            //      databaseUrl = "jdbc:mysql://127.0.0.1:3306/payment";
            databaseUsername = parser.getValue("database/username");
            databasePassword = parser.getValue("database/password");
           
        } catch (Exception e) {
            throw e;
        }
    }
   
    void loadPoolerConfiguration()
    throws WebSiteError {
        XMLParser parser = new XMLParser();
        if(!parser.parseFile( getConfPath() + java.io.File.separator + "pooler.xml" )) {
            throw new WebSiteError("Pooler information missing, Please setup database ","Click here to setup pooler","/conf/setup/");
        }
       
        //poolerSize = Integer.parseInt(parser.getValue("pooler/size"));
    }
   
    private void loadPooler()
    throws Exception {
        try {
            connections = new VectorEnum();
            for ( int i=0; i<10; i++) {
                DBConnection conn = new DBConnection(this);
                connections.addElement(conn);
            }
        } catch(Exception e) {
            throw e;
        }
    }
   
   
    public synchronized DBConnection getConnection() {
        return (DBConnection) connections.nextReset();
    }
0
a122178
Asked:
a122178
1 Solution
 
petmagdyCommented:
sorry I can't understand what closes automatically the JVM?

0
 
CEHJCommented:
Are you starting from the console? If so the process may be getting stopped. On Unix run with nohup in the background, on Windows run as a service
0
 
aozarovCommented:
You are not doing really connection pooling as your DataConnection#close calls the innerConnection.close(); and closes the real connection instead of pooling it.
Why to do it yourself. javakarta commons connection pool is good and easy to use:
http://jakarta.apache.org/commons/dbcp/
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
CEHJCommented:
How does that explain the automatic closing of the application?
0
 
a122178Author Commented:
I want to know the reason why the connection closes after a several time. Is there a default timeout period in the connection pool? The application stay forever.However,  only the connection to the database closes.
0
 
CEHJCommented:
>>The application stay forever.However,  only the connection to the database closes.

In that case i misunderstood your question
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now