Does Java have a using statement?

前端 未结 12 1941
梦谈多话
梦谈多话 2020-11-28 16:13

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()         


        
相关标签:
12条回答
  • 2020-11-28 17:01

    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.

    0 讨论(0)
  • 2020-11-28 17:01

    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.

    0 讨论(0)
  • 2020-11-28 17:04

    The closest java equivalent is

    AwesomeClass hooray = new AwesomeClass();
    try{
        // Great code
    } finally {
        hooray.dispose(); // or .close(), etc.
    }
    
    0 讨论(0)
  • 2020-11-28 17:07

    No, Java has no using statement equivalent.

    0 讨论(0)
  • 2020-11-28 17:07

    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);
        }
      }
    }
    
    0 讨论(0)
  • 2020-11-28 17:10

    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.

    0 讨论(0)
提交回复
热议问题