[HELP] How to handle SQL connections properly?

Support for the latest build of L2J Server, get help here with installations, upgrades, problems.
Do not post bugs reports here, use viewforum.php?f=77 instead.
There is no support for other server builds than the official provided by l2jserver.com
Forum rules
READ NOW: L2j Forums Rules of Conduct
Post Reply
Liarco
Posts: 7
Joined: Thu Oct 28, 2010 2:54 pm

[HELP] How to handle SQL connections properly?

Post 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!
User avatar
Zoey76
L2j Inner Circle
L2j Inner Circle
Posts: 7008
Joined: Tue Aug 11, 2009 3:36 am

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

Post 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.
Powered by Eclipse 4.34 🌌 | Eclipse Temurin 21 ☕ | MariaDB 11.3.2 🗃️ | L2J Server 2.6.3.0 - High Five 🚀

🔗 Join our Discord! 🎮💬
Liarco
Posts: 7
Joined: Thu Oct 28, 2010 2:54 pm

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

Post 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?
Post Reply