I am getting into ActiveJDBC at the moment, a very nice and useful framework, as far as I can tell. But I am having some problems with the JDBC-Connection management of it, as it attaches an opened connection to the current thread. That means, if I open the connection at the initialisation of my program, everything works fine; but if I instantiate a JFrame afterwards and try reading/wrtiting data from/to the database in an ActionListener for example, it will generate an error, since there is no connection attached to the dispatch thread.
How to solve this problem? I'd rather have just one connection, to which I can get access (via Base.connection()) all the time, instead of having one connection attached to each thread..
Thanks in advance
I would suggest that you implement action listener this way:
public class AJListenerAdapter implements ActionListener{
public void actionPerformed(ActionEvent e){
Base.open(...);
doPerform(ActionEvent e);
Base.close(...);
}
protected abstract doPerform(ActionEvent e);
}
then just subclass this adapter and implement the doPerform()
method.
Additionally you might want to use connections from a pool. Here is an example https://github.com/javalite/activejdbc/blob/master/activejdbc/src/test/java/org/javalite/activejdbc/C3P0PoolTest.java