• Post Reply Bookmark Topic Watch Topic
  • New Topic

Does Automatic Resource Management work for hibernate session also.

 
Naresh Chaurasia
Ranch Hand
Posts: 361
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have following working code for jdbc automatic resource management.



Does the resource management also work for hibernate session object.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15713
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Automatic resource management works for anything that implements interface java.lang.AutoCloseable.

So, you can lookup in the Hibernate API documentation if the Hibernate session class implements this interface. If it does, then you can use it in a try (...) block.
 
Ivan Jozsef Balazs
Rancher
Posts: 992
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
However you should first load the driver class (Class.forName ) and then obtain a connection.
 
Silviu Burcea
Ranch Hand
Posts: 61
1
IntelliJ IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you're using JDK 6 or higher, Class.forName call is not needed any more: http://docs.oracle.com/javase/6/docs/api/java/sql/DriverManager.html . Being on JDK 7 (first JDK with try-with-resources), I'd remove that call. It does not have any impact(to keep backwards compatibility), but you don't need it any more.
 
Rob Spoor
Sheriff
Posts: 20817
68
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That actually depends on the driver. If the driver does not support the service provider mechanism, then it won't be registered without any manual action (Class.forName). Most recent drivers support the service provider mechanism though.
 
Tim Holloway
Bartender
Posts: 18408
58
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor wrote:That actually depends on the driver. If the driver does not support the service provider mechanism, then it won't be registered without any manual action (Class.forName). Most recent drivers support the service provider mechanism though.


It would be a very antiquated (probably obsolete) driver these days. Or a sloppy one.

In fact, I THINK the mechanisms in question were first popularized about the time Java 4 came out.

However, obtaining Connections by direct brute force is a debatable policy when using an ORM system such as Hibernate. Unless you're strictly single-user/single-threaded, you should let a database connection pool manage the Connections.

Especially if you're doing this in a web application.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15713
73
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The service provider mechanism was added in Java 6, which included JDBC version 4.0 - and in JDBC 4.0 support for the service provider mechanism was added.

So if your driver is for Java 6 / JDBC 4.0 or later, it should support the service provider mechanism and it should not be necessary to load the driver with Class.forName(...).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!