Equivalent to Try-with-resources in Java 1.6

I have the following code:

public class Main { public static void main(String[] args) throws SQLException { try ( Connection conn = DBUtil.getConnection(DBType.HSQLDB); Statement stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); ResultSet rs = stmt.executeQuery("SELECT * FROM tours"); ) { DBUtil.getConnection(); } catch (SQLException e) { DBUtil.processException(e); } } } 

I use this code to retrieve data from a database. My problem is that I cannot use the Java 1.7 compiler and must use 1.6. How can I translate try-with-resources code for use with 1.6 compiler? What exactly happens in this special try block?

+6
source share
3 answers

Oracle explains how try-with-resources work here

TL DR of this:
There is no easy way to do this in Java 1.6. The problem is the absence of a forbidden field in Exception. You can either ignore this or hard code, which happens when both try parameters close different exceptions or create your own Exception hierarchy that has a suppressed field.

In the second case, the link above gives the correct way to do this:

  AutoClose autoClose = new AutoClose(); MyException myException = null; try { autoClose.work(); } catch (MyException e) { myException = e; throw e; } finally { if (myException != null) { try { autoClose.close(); } catch (Throwable t) { myException.addSuppressed(t); } } else { autoClose.close(); } } 

equivalently

 try (AutoClose autoClose = new AutoClose()) { autoClose.work(); } 

If you want to make it simpler and not create many new Exception classes, you have to decide what to do in catch at the end of finally (t or e).

PS. Working with multiple variable declarations in try is also discussed in the link above. And the amount of code you need for this is overwhelming. Most people use shortcuts in Java 1.6, not handling exceptions in the finally block and using nullchecks.

+7
source

Do it like this:

 Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = DBUtil.getConnection(DBType.HSQLDB); stmt = conn.createStatement( ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY); rs = stmt.executeQuery("SELECT * FROM tours"); } catch (SQLException e) { DBUtil.processException(e); } finally { if(conn != null) { conn.close(); } if(stmt != null) { stmt.close(); } if(rs != null) { rs.close(); } } 
+1
source

I would suggest using the apache commons-dbutils library , which have a DBUtils class with close and closeQuietly . The code will look like this:

 import org.apache.commons.dbutils.DBUtils; ... Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = myOwnUtil.getConnection(); stmt = conn.createStatement(); rs = stmt.executeQuery( "SELECT * FROM table" ); // or any other custom query } catch ( SQLException e ) { <<handle exception here>>; } finally { DBUtils.closeQuietly( conn ); DBUtils.closeQuietly( stmt ); DBUtils.closeQuietly( rs ); // or simply use DBUtils.close( conn, stmt, rs ); } 

Note that closeQuietly will not throw any exceptions, while close can use SQLException, so adapt the code to your own use case.

If you want to close threads, you can use apache commons-io with the IOUtils class, which also has close and closeQuietly.

0
source

Source: https://habr.com/ru/post/1257715/


All Articles