Page 1 of 1

[HELP] How to handle SQL connections properly?

Posted: Mon Nov 08, 2010 3:51 pm
by Liarco
If you want to receive support we need this info to help you properly.
» Find Revision
L2J Revision Number: 4422
L2JDP Revision Number: 7669

Hi all,
I'm a developer and I'm almost new to L2J. I would like to write some simple mods and scripts in order to learn more about the L2J system.
While i was looking for documentation, I found a lot of posts about "unclosed SQL connections" and I started to worry about them. I would like to write good code, so may you tell me what is the right way to connect to the database and close everything without any error? (both for java and python)

Here is a simple scheme of what I'm currently using, is it right?
Java scheme:
The main question is about the line 12... which one is the right close() method?

Code: Select all

try {    con = L2DatabaseFactory.getInstance().getConnection();    statement = con.prepareStatement("SQL QUERY");    rset = statement.executeQuery();            // ANY NEEDED OPERATION} catch (SQLException e) {        // HANDLE EXCEPTIONS} finally {    try    {        con.close(); // OR L2DatabaseFactory.close(con)????        statement.close();        rset.close();    } catch (Exception e) {        // HANDLE EXCEPTIONS    }}
Python scheme:
Here is an example posted by ThE_PuNiSheR: viewtopic.php?f=46&t=18071&p=95665&hili ... %3A#p96989

Thanks for all your great work!

Re: [HELP] How to handle SQL connections properly?

Posted: Mon Nov 08, 2010 11:13 pm
by Zoey76
You should use L2DatabaseFactory.close(con); because using con.close() you are hardcoding.

L2DatabaseFactory.close(con);

Code: Select all

Connection con = null;try{    con = L2DatabaseFactory.getInstance().getConnection();    PreparedStatement statement = con.prepareStatement("SQL QUERY");    ResultSet rset = statement.executeQuery();    // ANY NEEDED OPERATION}catch (SQLException e){    // HANDLE EXCEPTIONS}finally{    L2DatabaseFactory.close(con);}
con.close();

Code: Select all

Connection con = null;try{    con = L2DatabaseFactory.getInstance().getConnection();    PreparedStatement statement = con.prepareStatement("SQL QUERY");    ResultSet rset = statement.executeQuery();    // ANY NEEDED OPERATION}catch (SQLException e){    // HANDLE EXCEPTIONS}finally{    try    {        con.close();    }    catch (SQLException e)    {        // HANDLE EXCEPTIONS AT CLOSING    }}
This is L2DatabaseFactory.close(Connection con):

Code: Select all

public static void close(Connection con){    if (con == null)        return;        try    {        con.close();    }    catch (SQLException e)    {        _log.log(Level.WARNING, "Failed to close database connection!", e);    }}
As you can see it's actually doing a null check and the try/catch to close the connection.

Re: [HELP] How to handle SQL connections properly?

Posted: Tue Nov 09, 2010 2:26 am
by Liarco
Thank you so much! Just one more question: as I can see from your examples, it's useless to close the statement and the result set, is it right?