java7 Connection implements AutoCloseable

If something doesn't fit in any other forum then post it here.
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
User avatar
tukune
Posts: 533
Joined: Sun Mar 29, 2009 2:35 pm
Location: Japan

java7 Connection implements AutoCloseable

Post by tukune »

:D

Code: Select all

- java.sql.Connection con = null;- try- {-     con = L2DatabaseFactory.getInstance().getConnection();+ try (Connection con = L2DatabaseFactory.getInstance().getConnection())+ {      PreparedStatement statement = con.prepareStatement("SELECT * FROM `foo`");      ResultSet rs= statement.executeQuery();      .....  }  catch (Exception e) { ..... }- finally { L2DatabaseFactory.close(con); }
User avatar
jurchiks
Posts: 6769
Joined: Sat Sep 19, 2009 4:16 pm
Location: Eastern Europe

Re: java7 Connection implements AutoCloseable

Post by jurchiks »

That's good to know, but when does it close the connection?
If you have problems, FIRST TRY SOLVING THEM YOURSELF, and if you get errors, TRY TO ANALYZE THEM, and ONLY if you can't help it, THEN ask here.
Otherwise you will never learn anything if all you do is copy-paste!
Discussion breeds innovation.
User avatar
Stake
Posts: 383
Joined: Sun Mar 23, 2008 9:33 pm
Location: Hungary
Contact:

Re: java7 Connection implements AutoCloseable

Post by Stake »

jurchiks wrote:That's good to know, but when does it close the connection?
I think, after it jumped out of try block.
Image
Image
User avatar
JIV
L2j Veteran
L2j Veteran
Posts: 1882
Joined: Sun Jan 06, 2008 8:17 pm
Location: Slovakia
Contact:

Re: java7 Connection implements AutoCloseable

Post by JIV »

is it automatically added into bytecode?
User avatar
Stake
Posts: 383
Joined: Sun Mar 23, 2008 9:33 pm
Location: Hungary
Contact:

Re: java7 Connection implements AutoCloseable

Post by Stake »

JIV wrote:is it automatically added into bytecode?
More probably. It doesn't seem to be a runtime check. :D
Image
Image
Post Reply