-->
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.  [ 4 posts ] 
Author Message
 Post subject: No HibernateException if it fails to connect to the DB?
PostPosted: Tue Nov 22, 2005 11:28 am 
Newbie

Joined: Tue Nov 22, 2005 10:01 am
Posts: 7
Hibernate version: 2.1.6


Code being Executed
/**
* This just sets up a hibernate Session to ensure all the hibernate
* threads are created at startup.
* @throws HibernateException If there is an error when creating the session.
*/
private static void setupHibernateSession() throws Exception, HibernateException {
theLogger.debug("Entering MailReceiver.setupHibernateSession");
Session s = HibernateUtil.getSessionFactory().openSession();
s.close();
HibernateUtil.closeSession();
theLogger.debug("Exiting MailReceiver.setupHibernateSession");
return;
}

Full stack trace of any exception that occurs:
2005-11-22 14:59:58,265 INFO [main] (SettingsFactory.java:63) - Use outer join fetching: true
2005-11-22 14:59:58,312 INFO [main] (C3P0ConnectionProvider.java:48) - C3P0 using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://192.168.1.90/mailbrand
2005-11-22 14:59:58,328 INFO [main] (C3P0ConnectionProvider.java:49) - Connection properties: {user=*, password=*}
2005-11-22 14:59:58,718 INFO [main] (TransactionManagerLookupFactory.java:33) - No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@3bc20e [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@29ac [ acquireIncrement -> 2, autoCommitOnClose -> false, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, idleConnectionTestPeriod -> 300, initialPoolSize -> 10, maxIdleTime -> 5000, maxPoolSize -> 100, maxStatements -> 100, minPoolSize -> 10, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@1415056 [ description -> null, driverClass -> null, factoryClassLocation -> null, jdbcUrl -> jdbc:mysql://192.168.1.90/mailbrand, properties -> {user=paddy, password=paddy} ] , propertyCycle -> 300, testConnectionOnCheckout -> false ] , factoryClassLocation -> null, numHelperThreads -> 3 ]
java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.net.ConnectException: Connection refused: connect

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused: connect

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused: connect
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:143)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1805)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:66)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:76)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1.acquireResource(C3P0PooledConnectionPool.java:66)
at com.mchange.v2.resourcepool.BasicResourcePool.assimilateResource(BasicResourcePool.java:787)
at com.mchange.v2.resourcepool.BasicResourcePool.acquireUntil(BasicResourcePool.java:548)
at com.mchange.v2.resourcepool.BasicResourcePool.access$000(BasicResourcePool.java:35)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:972)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:148)


** END NESTED EXCEPTION **


at com.mysql.jdbc.Connection.createNewIO(Connection.java:1875)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:66)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:76)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1.acquireResource(C3P0PooledConnectionPool.java:66)
at com.mchange.v2.resourcepool.BasicResourcePool.assimilateResource(BasicResourcePool.java:787)
at com.mchange.v2.resourcepool.BasicResourcePool.acquireUntil(BasicResourcePool.java:548)
at com.mchange.v2.resourcepool.BasicResourcePool.access$000(BasicResourcePool.java:35)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:972)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:148)
java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.net.ConnectException: Connection refused: connect

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: java.net.ConnectException: Connection refused: connect

STACKTRACE:

java.net.SocketException: java.net.ConnectException: Connection refused: connect
at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:143)
at com.mysql.jdbc.MysqlIO.<init>(MysqlIO.java:225)
at com.mysql.jdbc.Connection.createNewIO(Connection.java:1805)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:66)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:76)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1.acquireResource(C3P0PooledConnectionPool.java:66)
at com.mchange.v2.resourcepool.BasicResourcePool.assimilateResource(BasicResourcePool.java:787)
at com.mchange.v2.resourcepool.BasicResourcePool.acquireUntil(BasicResourcePool.java:548)
at com.mchange.v2.resourcepool.BasicResourcePool.access$000(BasicResourcePool.java:35)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:972)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:148)


** END NESTED EXCEPTION **


at com.mysql.jdbc.Connection.createNewIO(Connection.java:1875)
at com.mysql.jdbc.Connection.<init>(Connection.java:452)
at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:411)
at java.sql.DriverManager.getConnection(DriverManager.java:525)
at java.sql.DriverManager.getConnection(DriverManager.java:140)
at com.mchange.v2.c3p0.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:66)
at com.mchange.v2.c3p0.WrapperConnectionPoolDataSource.getPooledConnection(WrapperConnectionPoolDataSource.java:76)
at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool$1.acquireResource(C3P0PooledConnectionPool.java:66)
at com.mchange.v2.resourcepool.BasicResourcePool.assimilateResource(BasicResourcePool.java:787)
at com.mchange.v2.resourcepool.BasicResourcePool.acquireUntil(BasicResourcePool.java:548)
at com.mchange.v2.resourcepool.BasicResourcePool.access$000(BasicResourcePool.java:35)
at com.mchange.v2.resourcepool.BasicResourcePool$AcquireTask.run(BasicResourcePool.java:972)
at com.mchange.v2.async.ThreadPoolAsynchronousRunner$PoolThread.run(ThreadPoolAsynchronousRunner.java:148)

MYSQL Version 4.1


Hi,
I'm trying to get my application to exit if it cannot connect to the database. I have a function, setupHibernateSession() (Top of Post) called early in main which just creates a session and closes it again. This is because creating the first session can be slow due to all the threads that are created so I want it done at startup before any queries need to be handled. Anyway if the Mysql service is not running for some reason a HibernateException is not thrown which is what I would have expected to happen. An Unhandled SocketException seems to be thrown. Even if I try to catch this SocketException it's not caught as I don't think the jre is expecting one to be thrown from this method. What sort of Exception should I be trying to catch? Any help would be much appreciated.

divo


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 23, 2005 3:23 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 3:00 pm
Posts: 1816
Location: Austin, TX
it is actually throwing a SQLException:
Quote:
java.sql.SQLException: Unable to connect to any hosts due to exception: java.net.SocketException: java.net.ConnectException: Connection refused: connect

** BEGIN NESTED EXCEPTION **
...


You need to understand that simply opening a Hibernate Session does not force it to acquire a connection. It delays obtaining a connection until one is actually needed.

The typical way to force this is via a call to session.connection()


Top
 Profile  
 
 Post subject:
PostPosted: Fri Nov 25, 2005 9:57 am 
Newbie

Joined: Tue Nov 22, 2005 10:01 am
Posts: 7
Thanks for the reply. Calling s.connection did cause an exception to be thrown which is very useful as my program now terminates rather than carrying on not worring a jot as to wether it can or can't access the DB or not...lol. Ok I'm curious if Session s = HibernateUtil.getSessionFactory().openSession(); does not actually connect to the DB then why are there no errors when the DB is running but there are when it si not running? Surely it must be connecting to the DB somnehow? Any javadocs on this u can point me too? Thanks very much for your help so far.

divo


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 28, 2005 11:14 am 
Newbie

Joined: Tue Nov 22, 2005 10:01 am
Posts: 7
I'm still not really happy with this as a solution. It causes a load of stack traces to be printed to the screen before if finally returns and my next line of code s.connection() causes a HibernateException to be thrown. According to the hibernate docs at:
http://www.hibernate.org/hib_docs/api/n ... enSession()

openSession() should create a DB connection and it indicates a HibernateException will be thrown if this method does not return successfully. Is this a hibernate bug?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.