Does Java have a using statement that can be used when opening a session in hibernate?
In C# it is something like:
using (var session = new Session())
{
}
So the object goes out of scope and closes automatically.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Java 7 introduced Automatic Resource Block Management which brings this feature to the Java platform. Prior versions of Java didn’t have anything resembling
using.As an example, you can use any variable implementing
java.lang.AutoCloseablein the following way:Java’s
java.io.Closeableinterface, implemented by streams, automagically extendsAutoCloseable, so you can already use streams in atryblock the same way you would use them in a C#usingblock. This is equivalent to C#’susing.As of version 5.0, Hibernate Sessions implement
AutoCloseableand can be auto-closed in ARM blocks. In previous versions of Hibernate Session did not implementAutoCloseable. So you’ll need to be on Hibernate >= 5.0 in order to use this feature.