need Clarity on com.j256.ormlite.support.ConnectionSource close

47 views
Skip to first unread message

preethi subbu

unread,
Feb 5, 2021, 1:27:32 AM2/5/21
to ORMLite Users

I am new to Ormlite. I need to close the connectionSource. Tried close. During query execution, connections are implicitly opened. How to prevent this. How to close the connection correctly?

Sample code:
import com.j256.ormlite.jdbc.JdbcConnectionSource;
import com.j256.ormlite.support.ConnectionSource;

ConnectionSource connectionSource = new JdbcConnectionSource(url + databaseName + "?currentSchema=trial&useSSL=false", userName, password);
connectionSource.close();

The below output shows that query are executed by same connection object even after closing it.
DaoManager created dao for class class com.zcpersistence.entity1508480.test with reflection
StatementExecutor query of 'SELECT COUNT(*) FROM `test`' returned 1
JdbcConnectionSource closed connection #1020391880
JdbcConnectionSource opened connection to jdbc:mysql:*********
StatementExecutor query of 'SELECT COUNT(*) FROM `test`' returned 1

Thanks ahead!

preethi subbu

unread,
Feb 7, 2021, 9:59:33 PM2/7/21
to ORMLite Users

It would be of great help if someone could show how to close connection source properly so that when old Dao object is used for querying, appropriate error should be thrown.

Gray Watson

unread,
Feb 8, 2021, 10:48:57 AM2/8/21
to ORMLite Users
Hi Preethi:

If you use the DAO with a JdbcConnectionSource, it will create a connection when needed. If you then close the JdbcConnectionSource it will close the connection if it had been opened. If you then make _another_ call to the DAO it will open another connection up. Is that what you don't want to happen? Right now it allows a connection to be open again.

You could subclass it and override close(), getReadOnlyConnection(String), and getReadWriteConnection(String) so that the get methods return null or throw an exception if close has been done previously or something.

Does that help?
gray
> --
> You received this message because you are subscribed to the Google Groups "ORMLite Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ormlite-user...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ormlite-user/e7114003-4974-4b60-b2d5-643459617697n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages