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.
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.
Sorry. No. Java does not have an equivalent to the using statement.
Not exactly the same but somewhat related, Java 7 (coming soon) will include Automatic Resource Block Management.
Technically:
DisposableObject d = null;
try {
d = new DisposableObject();
}
finally {
if (d != null) {
d.Dispose();
}
}
As of now, no.
However there is a proposal of ARM for Java 7.
Any code inside a code block in Java will go out of scope outside of the code block - so for example you could use
{
Session session = new Session();
// session is in scope here.
}
// session is now out of scope and will be cleaned up by the GC at some point.