Link to home
Start Free TrialLog in
Avatar of a122178
a122178

asked on

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();
    }
Avatar of petmagdy
petmagdy
Flag of Canada image

sorry I can't understand what closes automatically the JVM?

Avatar of CEHJ
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
ASKER CERTIFIED SOLUTION
Avatar of aozarov
aozarov

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
How does that explain the automatic closing of the application?
Avatar of a122178
a122178

ASKER

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.
>>The application stay forever.However,  only the connection to the database closes.

In that case i misunderstood your question