Connection problems.

48 views
Skip to first unread message

Francisco Chiotta

unread,
Jan 2, 2014, 3:23:45 PM1/2/14
to activejd...@googlegroups.com
Hello everybody, 
                        I am beginner in activejdbc, and I am learning how to use this fantastic tool, I think it's very good.
I am facing a problem with connections. I am developing a swing application which retrieves information from a postgres database. Since swing application are multi-threads, I don't know how to manage it. Should I reopen a connection every time I make a DB operation? My app performs DB operations depending on events from the GUI, and I use SwingWorker(new thread) to do the work, so the connection(previously opened) is not active in this new thread.

Cheers.

Igor Polevoy

unread,
Jan 2, 2014, 4:10:15 PM1/2/14
to activejd...@googlegroups.com
If I were to build an app like that, I would use an in-memory database connection pool. 

I would then:

1. Implement my simple DataSource and used this method: http://javalite.github.io/activejdbc/org/javalite/activejdbc/Base.html#open(javax.sql.DataSource) to open a connection from data source. 
The custom datasource would pull a connection from  the pool. 
2. I would write one  class for all database access, and hide model code inside methods of that class. Think of this as a data access facade
For instance: 
3. How to open connection: use Java Proxy feature to get a new implementation of the PeopleFacade class, but the one that can open a connection before method, and close one after the method: 

Here is code example that does not use models, or actually opens connection, but it gives you a clue for my suggestion:

Simply use code in DatabaseConnectionHandler  to open a connection before method execution and close after. The connection itself can be opened directly or sourced from a pool

hope this helps

tx

Francisco Chiotta

unread,
Jan 3, 2014, 5:15:54 PM1/3/14
to activejd...@googlegroups.com
Igor, thank you very much for your help. It helped a lot.

Cheers.

Igor Polevoy

unread,
Jan 5, 2014, 1:09:53 AM1/5/14
to activejd...@googlegroups.com
Glad this helps, good luck
Reply all
Reply to author
Forward
0 new messages