File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes Database connectivity through hibernate Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » Object Relational Mapping
Bookmark "Database connectivity through hibernate" Watch "Database connectivity through hibernate" New topic
Author

Database connectivity through hibernate

Manuj Roy
Greenhorn

Joined: Feb 17, 2011
Posts: 7

Hi,

Can Hibernate connect rdbms automatically if the database gets down and let's say come up after 1 minute.

Thanks,
Manuj
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42932
    
  68
Depends how you use it. If it's configured to use an underlying connection pool, then probably yes - these days, most pools can detect an unavailable DB and recover from that pretty quickly.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2448
    
  28

The connection pool should be able to recover any inactive connections, but all the active connections would drop as soon as the database goes down. This means that any code that is calling Hibernate will get an exception. It won't be able to recover from a connection dropped on an active connection. You will have to either manage that in the application, or show error to user.
Manuj Roy
Greenhorn

Joined: Feb 17, 2011
Posts: 7

Thanks Friends, but I am little confused with the answer, you have mentioned that most pools can detect unavailable db and recover from it soon, but does it reconnect to db again if it db comes up and if yes what is underlying process/ api which does it and can we configure this in hibernate properties or cfg file?

Thanks, manuj
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42932
    
  68
That depends on the connection pool being used. To that extent I disagree with what Jayesh said - there are certainly pool implementations out there that know how to recover from an unavailable DB. I don't know if the default pool used by Hibernate can, but it's easy enough to test.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2448
    
  28

Maybe I didn't explain myself correctly. By "active" connections, I mean connections that are currently in an transaction. If the database goes down during a transaction, the transaction will roll back on the server, and Hibernate will most probably get an exception from underlying JDBC provider. Unless the JDBC provider has some way of keeping the transaction on the client side, and replaying it when the database comes back up, Hibernate cannot do anything

Now, it gets a little fuzzy, because Hibernate internally does caching in the session, and doesn't go to the database until the session is flushed. Atleast, I'm very fuzzy about it. Generally, the assumption that I make that if there is some catastrophic failure that causes the database to go down, or cause the connection to terminate, the Hibernate session will throw an exception.
Manuj Roy
Greenhorn

Joined: Feb 17, 2011
Posts: 7

Thanks a lot Ulf Dittmer and Jayesh, you have mentioned about a condition where a transaction is happening, but if we talk about a system in stand by mode, can hibernate reconnect to db in that mode as well?

Please suggest and if you know something about if we can control this mechnanism, please do write that too..

Thanks,
Manuj
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2448
    
  28

Yes, if there are no active transactions, then the connection pool should recover. It depends on the connection pool. Which connection pool are you using?
Manuj Roy
Greenhorn

Joined: Feb 17, 2011
Posts: 7

Thanks Jayesh for your reply, I am using c3p0.
Jayesh A Lalwani
Bartender

Joined: Jan 17, 2008
Posts: 2448
    
  28

C3P0 will recover from lost connections. Read this
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Database connectivity through hibernate