Does Java have a using statement?
Asked Answered
C

12

126

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.

Ceremonial answered 6/1, 2010 at 21:15 Comment(9)
"allowing to define a scope for an object" That's not what using does. Scope is not lifetime (and using isn't about lifetime either, strictly speaking, as Dispose does not destroy an object's memory.)Twitty
@Twitty Your comment is getting up-voted but I could do with a little more info. You're the one introducing the "lifetime" idea then you say it is not about "lifetime". Scope is the term used in the definition from the msdn library, maybe I misused it. How would you define the using statement.Baldric
Scope refers to the the area in code in which you can refer to an identifier without using its fully qualified name (local variable, type, method name, and such). Lifetime refers to the time in which an object or variable is accessible. See blogs.msdn.com/b/ericlippert/archive/2009/08/03/…Twitty
So for example, if you have a local variable and assign a value type instance to it, then the lifetime of your value will end when the lifetime of its variable ends. But if you allocated an object, and stored a reference to it in a local, then the lifetime of that object may very well extend past the lifetime of its storage, as long as there is still some reference to the object elsewhere. As for using, it automatically disposes the object at the end of its scope, but it does not deallocate the object – its lifetime is not over until all its references have disappeared.Twitty
possible duplicate of "using" keyword in javaOrdination
@Ordination So this is a duplicate of a question, that asked 5 months after this one? Shouldn't it be the other way around?Treenatreenail
ohh my mistake...I read it Jun..Thanks. I retract my close voteOrdination
@Ordination But it would be funny if both question are closed with the other one as the duplicate :D.Treenatreenail
may be SO get new bug in that case. Nice:- ))Ordination
P
147

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.AutoCloseable in the following way:

try(ClassImplementingAutoCloseable obj = new ClassImplementingAutoCloseable())
{
    ...
}

Java's java.io.Closeable interface, implemented by streams, automagically extends AutoCloseable, so you can already use streams in a try block the same way you would use them in a C# using block. This is equivalent to C#'s using.

As of version 5.0, Hibernate Sessions implement AutoCloseable and can be auto-closed in ARM blocks. In previous versions of Hibernate Session did not implement AutoCloseable. So you'll need to be on Hibernate >= 5.0 in order to use this feature.

Plaided answered 6/1, 2010 at 21:17 Comment(7)
"Luckily" with Java 7 being available now, this answer is no longer true (and I think that ARM blocks are exactly what using does).Fenella
@Joachim Sauer: Thanks. I updated my answer to reflect the passage of time. To your point of ARM blocks being exactly what using does; at the time I wrote this answer, it looked to me like ARM blocks had to be try blocks, whereas using can be applied to any arbitrary block. Looking at it now, it seems as though the do keyword can be used in java to accomplish this. Was that added to the proposal recently or did I miss it the first time? Also the OP asked specifically about Hibernate Sessions. AFAIK: Hibernate Sessions still don't implement AutoCloseable so they can't use ARM yet.Plaided
Not event in 4.3 Hibernate DOES NOT implement AutoCloseable. docs.jboss.org/hibernate/orm/4.3/javadocs/index.html?org/… I guess it's up to everyone to write its own wrapper?Chemosh
Session cannot implement AutoCloseable as Session.close() returns a Connection. I think this is a bad design but I doubt this will be ever changed.Faroff
@usr-local-ΕΨΗΕΛΩΝ I just figured they'd be obliging anyone using hibernate to switch to java 7 if they implemented that interface. AutoCloseable didn't exist prior to java 7 did it?Precursory
Correct, but if they still implement java.io.Closeable you get the try statement in Java 7 codeFaroff
Updating the discussion. :) docs.jboss.org/hibernate/orm/5.0/javadocs/index.html?org/…Damson
F
33

Before Java 7, there was no such feature in Java (for Java 7 and up see Asaph's answer regarding ARM).

You needed to do it manually and it was a pain:

AwesomeClass hooray = null;
try {
  hooray = new AwesomeClass();
  // Great code
} finally {
  if (hooray!=null) {
    hooray.close();
  }
}

And that's just the code when neither // Great code nor hooray.close() can throw any exceptions.

If you really only want to limit the scope of a variable, then a simple code block does the job:

{
  AwesomeClass hooray = new AwesomeClass();
  // Great code
}

But that's probably not what you meant.

Fenella answered 31/5, 2010 at 12:39 Comment(9)
Your Java equivalent should be no problem if // Great code throws an exception.Confront
When the constructor throws an exception, I think your code is going to result in a NullPointerException that masks the original exception.Paripinnate
@Michael: actually my example would not compile, because horray may not have been initialized at that point (fixed now).Fenella
+1 for floating simple blocks being able to limit scope. However, whenever I see these it's almost always an indicator that the method should be broken up into smaller chunks.Outlander
Why cannot you move 'new AwesomeClass()' to the first line and avoid null checking in 'finally' block?Germaine
@ironic: if you move the new AwesomeClass() outside the try then the finally won't execute if that fails, if you move the declaration inside the try then you still need to check for null (because if the constructor throws an exception, then the assignment never happens).Fenella
@Joachim: thanks for trying to explain that. However, could you please provide details on what exactly could fail when you move constructor call outside the 'try' block? To me it seems that only constructor call may fail there, and if that happens we do not need to execute the 'finally' block because there is no chance that we will have new object assigned to anything... Am I missing something?Germaine
@Germaine you're right, what Joachim Sauer is doing is an anti-pattern, IMHO. When a constructor fails it throws an exception, not return null. Michael Borgwardt's answer is the way to go.Marigraph
if you want to catch any exception from the constructor, you have to have it inside the try block. it would be cumbersome to wrap the whole thing in another try/catch.Adulthood
O
20

Since Java 7 it does: http://blogs.oracle.com/darcy/entry/project_coin_updated_arm_spec

The syntax for the code in the question would be:

try (Session session = new Session())
{
  // do stuff
}

Note that Session needs to implement AutoClosable or one of its (many) sub-interfaces.

Otti answered 24/8, 2011 at 8:17 Comment(0)
T
8

Technically:

DisposableObject d = null;
try {
    d = new DisposableObject(); 
}
finally {
    if (d != null) {
        d.Dispose();
    }
}
Tremble answered 6/1, 2010 at 21:18 Comment(3)
Not the best way to do it. #1910162Dyspeptic
This would essentially be equivalent. I don't care if it is the best way.Tremble
Written like a true C# programmer. ;)Precursory
P
8

The closest java equivalent is

AwesomeClass hooray = new AwesomeClass();
try{
    // Great code
} finally {
    hooray.dispose(); // or .close(), etc.
}
Paripinnate answered 31/5, 2010 at 12:40 Comment(0)
T
3

As of now, no.

However there is a proposal of ARM for Java 7.

Taconite answered 6/1, 2010 at 21:20 Comment(0)
M
3

If you're interested in resource management, Project Lombok offers the @Cleanup annotation. Taken directly from their site:

You can use @Cleanup to ensure a given resource is automatically cleaned up before the code execution path exits your current scope. You do this by annotating any local variable declaration with the @Cleanup annotation like so:

@Cleanup InputStream in = new FileInputStream("some/file");

As a result, at the end of the scope you're in, in.close() is called. This call is guaranteed to run by way of a try/finally construct. Look at the example below to see how this works.

If the type of object you'd like to cleanup does not have a close() method, but some other no-argument method, you can specify the name of this method like so:

@Cleanup("dispose") org.eclipse.swt.widgets.CoolBar bar = new CoolBar(parent, 0);

By default, the cleanup method is presumed to be close(). A cleanup method that takes argument cannot be called via @Cleanup.

Vanilla Java

import java.io.*;

public class CleanupExample {
  public static void main(String[] args) throws IOException {
    InputStream in = new FileInputStream(args[0]);
    try {
      OutputStream out = new FileOutputStream(args[1]);
      try {
        byte[] b = new byte[10000];
        while (true) {
          int r = in.read(b);
          if (r == -1) break;
          out.write(b, 0, r);
        }
      } finally {
        out.close();
      }
    } finally {
      in.close();
    }
  }
}

With Lombok

import lombok.Cleanup;
import java.io.*;

public class CleanupExample {
  public static void main(String[] args) throws IOException {
    @Cleanup InputStream in = new FileInputStream(args[0]);
    @Cleanup OutputStream out = new FileOutputStream(args[1]);
    byte[] b = new byte[10000];
    while (true) {
      int r = in.read(b);
      if (r == -1) break;
      out.write(b, 0, r);
    }
  }
}
Mathi answered 31/5, 2010 at 13:13 Comment(0)
G
2

No, Java has no using statement equivalent.

Gunslinger answered 6/1, 2010 at 21:18 Comment(1)
It has now: oracle.com/technetwork/articles/java/…Oneida
P
2

In java 8 you can use try. Please refer to following page. http://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

Perreira answered 13/8, 2014 at 13:56 Comment(0)
S
1

Please see this List of Java Keywords.

  1. The using keyword is unfortunately not part of the list.
  2. And there is also no equivalence of the C# using keyword through any other keyword as for now in Java.

To imitate such "using" behaviour, you will have to use a try...catch...finally block, where you would dispose of the resources within finally.

Sanguinary answered 31/5, 2010 at 12:41 Comment(5)
The fact that using is not a keyword doesn't mean a thing. The same feature can be (and will be!) implemented with another keyword, as @BalusC mentioned.Fenella
I agree! But for now, it doesn't exist, right? That is what the OP asked, if there was something alike just now. It is good to know that it will exists in future releases, but that doesn't change a thing as for now, one way or another. Anyway, the information provided by @BalusC is great though! =)Sanguinary
I agree with that, but your post seems to say that the fact that using is not in the list of Java keywords means that this feature is not present in the Java language. And that's not true.Fenella
If this is what my post seems to say, then I will edit to reflect my intention.Sanguinary
I edited my answer specifying that there was no using keyword, and neither any equivalence as for now. Thanks @Joachim Sauer! =)Sanguinary
H
1

ARM blocks, from project coin will be in Java 7. This is feature is intended to bring similar functionality to Java as the .Net using syntax.

Heartbeat answered 31/5, 2010 at 12:42 Comment(0)
M
0

To answer the question regarding limiting scope of a variable, instead of talking about automatically closing/disposing variables.

In Java you can define closed, anonymous scopes using curly brackets. It's extremely simple.

{
   AwesomeClass hooray = new AwesomeClass()
   // Great code
}

The variable hooray is only available in this scope, and not outside it.

This can be useful if you have repeating variables which are only temporary.

For example, each with index. Just like the item variable is closed over the for loop (i.e., is only available inside it), the index variable is closed over the anonymous scope.

// first loop
{
    Integer index = -1;
    for (Object item : things) {index += 1;
        // ... item, index
    }
}

// second loop
{
    Integer index = -1;
    for (Object item : stuff) {index += 1;
        // ... item, index
    }
}

I also use this sometimes if you don't have a for loop to provide variable scope, but you want to use generic variable names.

{
    User user = new User();
    user.setId(0);
    user.setName("Andy Green");
    user.setEmail("[email protected]");
    users.add(user);
}

{
    User user = new User();
    user.setId(1);
    user.setName("Rachel Blue");
    user.setEmail("[email protected]");
    users.add(user);
}
Marigraph answered 26/9, 2013 at 13:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.