B4J Question Downside to not closing MySQL connections?

GuyBooth

Active Member
Licensed User
Longtime User
I open a SQL connection each time a make an async SQL call or sometimes a group of async calls. Each time I open a connection, I try to close it after the call is complete, but there are occasions when this results in an error where I am trying to run a SQL request against a closed database.
B4X:
java.sql.SQLException: execute() is called on closed connection
Is there a downside to not closing each open connection? I would still open a connection for each call to make sure I have a valid connection.
 

GuyBooth

Active Member
Licensed User
Longtime User
I recommend you to use jConnectionPool. This way you will not need to manage the connections and you will always get a valid connection.
Ok … is there any documentation for using jConnectionPool? Or examples? I couldn't find any, perhaps I am looking in the wrong place...
I have got as far as initializing with
B4X:
    CP.Initialize(driver, jdbcUrl, Username, Password)
However my next step
B4X:
    CP.GetConnectionAsync("sqlMB")
    wait for sqlMB_ConnectionReady(Success As Boolean, sqlMB As SQL)
throws a compiler error "Parameter name cannot hide global variable name"
My sqlMB is the global name for the SQL I'm using throughout the application, so I'm not sure what the connection pool is looking for here.

My original question also still stands. The jConnectionPool.getConnection help bubble says "Make sure to close the connection when done with it" but the jConnectionPool.getConnectionAsync does not. Do I need to close each connection when I use jConnectionPool.getConnectionAsync? What happens if I don't?
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
just change the name in the wait for line...
B4X:
wait for sqlMB_ConnectionReady(Success As Boolean, sql As SQL)
 
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
just change the name in the wait for line...
B4X:
wait for sqlMB_ConnectionReady(Success As Boolean, sql As SQL)
What do I do with the SQL returned - ignore it?
[Edit] No that doesn't work. This works:
B4X:
sqlMB = sql_MB
'or when returning (Success, SQL)
sqlMB = SQL

but I'm not sure why I'm doing it.
 
Last edited:
Upvote 0

GuyBooth

Active Member
Licensed User
Longtime User
Don't have a global SQL variable.
Work with the local one and close it when done.

This way you let the connection pool take care of managing the remote connections.
OK, got it. Thank you.
 
Upvote 0
Top