-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 
Author Message
 Post subject: "Sleeping" MYSQL Database?
PostPosted: Mon Apr 26, 2004 2:02 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
<takes note of the new format of questions.>

Hi all, been a while since I last visited, but got an interesting little issue, that I believe is NOT Hibernate's fault, but since it involves Hibernate, I figured that I'd at least check with you guys to make sure that I've at least done the Hibernate part right. :)

Hibernate Version: 2.0

I'm not going to post my mapping docs, as I know they are right (quite simple ones), but I'll post the first few lines of my hibernate.cfg.xml files.

This is my cfg for a MYSQL database of version 4.0.17-nt
Code:
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>

And this is my cfg for a MYSQL database of version 3.23.57-nt
Code:
<hibernate-configuration>
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>

As you can see, they're identical.

Now here's the exception stacktrace, it's rather long, as it's a result of the first one, being caught, and then trying to handle, and that failing too, etc.
Code:
pr 22, 2004 8:03:25 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 0, SQLState: 08003
Apr 22, 2004 8:03:25 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: No operations allowed after connection closed
Apr 22, 2004 8:03:25 PM net.sf.hibernate.JDBCException <init>
SEVERE: Could not load object
java.sql.SQLException: No operations allowed after connection closed
        at com.mysql.jdbc.Connection.checkClosed(Connection.java:2497)
        at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1287)
        at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1267)
        at net.sf.hibernate.impl.SessionFactoryImpl.getPreparedStatement(SessionFactoryImpl.java:543)
        at net.sf.hibernate.impl.BatcherImpl.prepareQueryStatement(BatcherImpl.java:50)
        at net.sf.hibernate.loader.Loader.prepareQueryStatement(Loader.java:395)
        at net.sf.hibernate.loader.Loader.doFind(Loader.java:118)
        at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:479)
        at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:37)
        at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:386)
        at net.sf.hibernate.impl.SessionImpl.doLoad(SessionImpl.java:1873)
        at net.sf.hibernate.impl.SessionImpl.doLoadByClass(SessionImpl.java:1741)
        at net.sf.hibernate.impl.SessionImpl.load(SessionImpl.java:1673)
        at dse6.processXML.ProcessXML.doPartyMatter(ProcessXML.java:215)
        at dse6.processXML.ProcessXML.saveXML(ProcessXML.java:54)
        at dse6.processXML.ProcessXML.xsd(ProcessXML.java:561)
        at dse6.processXML.ProcessXML.xsd(ProcessXML.java:539)
        at dse6.utility.Transform.xmlTransform(Transform.java:59)
        at dse6.DSE6Core.run(DSE6Core.java:90)
        at dse6.DSE6Runner$RunDSE6.run(DSE6Runner.java:110)
        at java.util.TimerThread.mainLoop(Timer.java:432)
        at java.util.TimerThread.run(Timer.java:382)
Apr 22, 2004 8:03:25 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
WARNING: SQL Error: 0, SQLState: 08003
Apr 22, 2004 8:03:25 PM net.sf.hibernate.util.JDBCExceptionReporter logExceptions
SEVERE: No operations allowed after connection closed
Apr 22, 2004 8:03:25 PM net.sf.hibernate.JDBCException <init>
SEVERE: Could not insert
java.sql.SQLException: No operations allowed after connection closed
        at com.mysql.jdbc.Connection.checkClosed(Connection.java:2497)
        at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1287)
        at com.mysql.jdbc.Connection.prepareStatement(Connection.java:1267)
        at net.sf.hibernate.impl.SessionFactoryImpl.getPreparedStatement(SessionFactoryImpl.java:543)
        at net.sf.hibernate.impl.BatcherImpl.prepareStatement(BatcherImpl.java:46)
        at net.sf.hibernate.persister.EntityPersister.insert(EntityPersister.java:468)
        at net.sf.hibernate.impl.SessionImpl.doSave(SessionImpl.java:719)
        at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:607)
        at dse6.processXML.ProcessXML.doPartyMatter(ProcessXML.java:300)
        at dse6.processXML.ProcessXML.saveXML(ProcessXML.java:54)
        at dse6.processXML.ProcessXML.xsd(ProcessXML.java:561)
        at dse6.processXML.ProcessXML.xsd(ProcessXML.java:539)
        at dse6.utility.Transform.xmlTransform(Transform.java:59)
        at dse6.DSE6Core.run(DSE6Core.java:90)
        at dse6.DSE6Runner$RunDSE6.run(DSE6Runner.java:110)
        at java.util.TimerThread.mainLoop(Timer.java:432)
        at java.util.TimerThread.run(Timer.java:382)
Apr 22, 2004 8:03:25 PM net.sf.hibernate.transaction.JDBCTransaction rollback
SEVERE: Rollback failed
java.sql.SQLException: No operations allowed after connection closed
        at com.mysql.jdbc.Connection.checkClosed(Connection.java:2497)
        at com.mysql.jdbc.Connection.rollback(Connection.java:1378)
        at net.sf.hibernate.transaction.JDBCTransaction.rollback(JDBCTransaction.java:79)
        at dse6.processXML.ProcessXML.doPartyMatter(ProcessXML.java:339)
        at dse6.processXML.ProcessXML.saveXML(ProcessXML.java:54)
        at dse6.processXML.ProcessXML.xsd(ProcessXML.java:561)
        at dse6.processXML.ProcessXML.xsd(ProcessXML.java:539)
        at dse6.utility.Transform.xmlTransform(Transform.java:59)
        at dse6.DSE6Core.run(DSE6Core.java:90)
        at dse6.DSE6Runner$RunDSE6.run(DSE6Runner.java:110)
        at java.util.TimerThread.mainLoop(Timer.java:432)
        at java.util.TimerThread.run(Timer.java:382)
dse6.utility.IncidentException: net.sf.hibernate.JDBCException: Could not insert: No operations allowed after connection
closed
        at dse6.processXML.ProcessXML.doPartyMatter(ProcessXML.java:346)
        at dse6.processXML.ProcessXML.saveXML(ProcessXML.java:54)
        at dse6.processXML.ProcessXML.xsd(ProcessXML.java:561)
        at dse6.processXML.ProcessXML.xsd(ProcessXML.java:539)
        at dse6.utility.Transform.xmlTransform(Transform.java:59)
        at dse6.DSE6Core.run(DSE6Core.java:90)
        at dse6.DSE6Runner$RunDSE6.run(DSE6Runner.java:110)
        at java.util.TimerThread.mainLoop(Timer.java:432)
        at java.util.TimerThread.run(Timer.java:382)

Here's the code where the error happens, I'll post the Database connectivity bit afterwards.
Code:
            session = Database.getSession(DSESettings.getRccName());
            tx = session.beginTransaction();

            try {
                props = matter.getProperties();
                for (int i = 0; i < props.getProperty().size(); i++) {
                    property = (Property) props.getProperty().get(i);
                    try {
                  if ((!session.isOpen()) || (!session.isConnected())) {
                     session.reconnect();
                  }
                     tempProp = (Property) session.load(PropertyImpl.class, property.getVALID());
                        tempProp.setENTITYX(property.getENTITYX());
                        tempProp.setERFN(property.getERFN());
                        tempProp.setPRPTYDESCX(property.getPRPTYDESCX());
                        tempProp.setSTREETADDRESSX(property.getSTREETADDRESSX());
                        tempProp.setSUBURBX(property.getSUBURBX());
                        tempProp.setTOWNX(property.getTOWNX());
                        tempProp.setDeleteFlag(new Integer(0));
                        session.update(tempProp);
                    }
                    catch (JDBCException jdbce) {
                       newProperty = true;
                       if ((!session.isOpen()) || (!session.isConnected())) {
                          session.reconnect();
                       }
                  tempProp = property;
                  tempProp.setDeleteFlag(new Integer(0));
                  session.save(tempProp);
                    }
               catch (ObjectNotFoundException onfe) {
                  newProperty = true;
   if ((!session.isOpen()) || (!session.isConnected())) {
                     session.reconnect();
                  }
   tempProp = property;
   tempProp.setDeleteFlag(new Integer(0));
   session.save(tempProp);
               }
                    catch (HibernateException he) {
                  newProperty = true;
                  if ((!session.isOpen()) || (!session.isConnected())) {
                     session.reconnect();
                  }
                        tempProp = property;
                        tempProp.setDeleteFlag(new Integer(0));
                        session.save(tempProp);
                    }
               catch (Exception e) {
                  newProperty = true;
                  if ((!session.isOpen()) || (!session.isConnected())) {
                     session.reconnect();
                  }
                  tempProp = property;
                  tempProp.setDeleteFlag(new Integer(0));
                  session.save(tempProp);
               }
                    valID = tempProp.getVALID();
                }
            }
            catch (NullPointerException npe) {}

And here's the Database bit:
Code:
    public static Session getSession(String factory) throws HibernateException {

        Session session = null;

        if (factory==DSESettings.getRccName()) {
            if (rccSessionFactory!=null) {
                session = rccSessionFactory.openSession();
            }
            else {
                initialiseSessionFactory(DSESettings.getRccName());
                session = rccSessionFactory.openSession();
            }
        }
        else if (factory==DSESettings.getLawnetName()) {
            if (lawnetSessionFactory!=null) {
                session = lawnetSessionFactory.openSession();
            }
            else {
                initialiseSessionFactory(DSESettings.getLawnetName());
                session = lawnetSessionFactory.openSession();
            }
        }
        else if (factory==DSESettings.getFilesName()) {
            if (filesSessionFactory!=null) {
                session = filesSessionFactory.openSession();
            }
            else {
                initialiseSessionFactory(DSESettings.getFilesName());
                session = filesSessionFactory.openSession();
            }
        }
        return session;
    }

Now the problem definition. :)

The app that controls this is on a Timer (as you may have noticed from the stacktrace), which wakes up every few minutes and extracts files from one database (ver 3.23), parses them (XML format messages) and then stores them in another database (4.0). The problem seems to occur, that after a prolonged period of no connectivity, the sessionfactory for the RCC database appears to lose all connectivity, i.e. it starts to throw these errors. However, the 3.23 database is continually being accessed by other apps and its connectivity never falters.

Is it possible, that a database can go to "sleep" if it hasn't been accessed for a prolonged period? And how could I get it to wake up. :) Forgive the numerous catch statements, but I've been trying to stop this happening. If I run the same files again after just restarting the app, they run fine, just I'm a little stumped with this one. If it is that the database goes to sleep, I guess I could run a simple query to poll it every few minutes (I'll set this up to run overnight in the mean time).

Thanks for any assistance in advance.

-G


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 2:11 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Sorry, I left out that this line in the stacktrace:
Code:
dse6.processXML.ProcessXML.doPartyMatter(ProcessXML.java:215)

refers to the line:
Code:
tempProp = (Property) session.load(PropertyImpl.class, property.getVALID());


The next level exception occured when the app was trying to save any children (code not show). But that was a session.save() call, hence the "could not insert".

-G


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 2:28 am 
Regular
Regular

Joined: Wed Mar 03, 2004 9:38 am
Posts: 70
Personally, I don't use MySQL (<troll>I use a real database instead, PostgreSQL</troll> :-) ), but I remember reading somewhere that MySQL has a habit of closing idle connections. The recommended solution is to put some connection validation stuff in your connection pool configuration, i.e. when you retrieve a connection the pool does some simple query to make sure that the connection really is open and if not, close the connection and open another one.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 2:46 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
joib wrote:
Personally, I don't use MySQL (<troll>I use a real database instead, PostgreSQL</troll> :-)

Sometimes we just can't choose...
But if, what you said is true, then all I have to do is make that part of the code that queries the first DB for new records, also query the second if 0 rows are returned, just to keep it "alive".

-G


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 3:32 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
I'd rather recommend to use the autoReconnect=true Option in your JDBC connection URL.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 3:59 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
michael wrote:
I'd rather recommend to use the autoReconnect=true Option in your JDBC connection URL.

Thanks for the idea Michael, I did a quick search on autoReconnect in the FAQ and couldn't find anything.

If this is my cfg.xml section:

Code:
    <session-factory>
        <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
        <property name="dialect">net.sf.hibernate.dialect.MySQLDialect</property>
        <property name="connection.url">jdbc:mysql://0.0.0.0/rcc</property>
        <property name="connection.username">...</property>
        <property name="connection.password">...</property>
        <property name="show_sql">false</property>


Where would I put autoReconnect?

like this?
Code:
name="connection.url">jdbc:mysql://0.0.0.0/rcc;autoReconnect-true</property>

Or is it another property?

-G


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 4:04 am 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
Yes, like this, i think the seperator is a ? though. Read the docs for the mysql jdbc driver.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Apr 26, 2004 4:13 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Ok, I found it in the MYSQL documentation:
Quote:
The first property needs to be preceeded with a '?' character, and additional name-value pair properties are separated by an '&' character.

Thanks Michael, off to make this part of every URL. :)

-G


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 28, 2004 1:42 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Bad news, the autoreconnect=true didn't work.

So I'm going to try my "ping" idea as see how that works.

-G


Top
 Profile  
 
 Post subject:
PostPosted: Wed Apr 28, 2004 4:17 am 
CGLIB Developer
CGLIB Developer

Joined: Thu Aug 28, 2003 1:44 pm
Posts: 1217
Location: Vilnius, Lithuania
One of good ways is not to use any pool if web server and db are on the same machine, the last time I have used msql (3.x) connection startup was very fast and there is no performance problems if you do not use something stupid like connection per method, and I am sure it will not be any performance problems for this kind of timer even DB is on remote machine.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 29, 2004 6:54 am 
Senior
Senior

Joined: Tue Sep 23, 2003 8:18 am
Posts: 137
Location: Johannesburg, South Africa
Ok...so the "ping" method didn't work either...

<sobs quietly to himself>

So, I've gone with trying to recreate the whole SessionFactory everytime the thread wakes up (10 minutes).

<runs a test (+- 5 hours after implementing and running this)>

Ok...if I recreate the SessionFactory it works. Annoying...

-G


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 11 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.