I'm using activejdbc for a Java ee project I have to do for school and I really like it. I do however have a recurring problem whenever I need more than one connection to the same database in one HttpServletRequest.
The scenario is the following: I have a custom Tag which lists the contents of the shoppingcart of a user which is called on every request, since I placed it in my header.jsp. In this tag I ask activeJdbc for the contents of the shoppingcart of this user. It looks like this:
Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://localhost/wpr_webshop", "root", "root");
Iterator<Record> rIter = shopCart.getAll(Record.class).iterator();
if (rIter.hasNext()) {
int shoppingCartTotal = 0;
out.write("<div class='shoppingcartcontents'>Your cart contains:<br>");
while (rIter.hasNext()) {
Record r = rIter.next();
Artist a = r.parent(Artist.class);
out.write(a.getString("artist_name") + " - " + r.getString("title") + "<br />");
shoppingCartTotal += r.getInteger("price");
}
Base.close();
Then, in a servlet which renders a jsp that includes the jsp which uses the tag from above, I do additional stuff with activejdbc like normal record selection; also using Base.open()
on the same database which gives me a
org.javalite.activejdbc.DBException: Cannot open a new connection because existing connection is still on current thread, name: default, connection instance: com.mysql.jdbc.JDBC4Connection@2c2552bd. This might indicate a logical error in your application.
exception. I get what the problem here is and I'm already really thorough on closing the base connection as soon as I don't require it anymore, I can't however seem to prevent this problem.
I guess a solution to this would be to allow the custom tag, which is called on every request, use a different connection to not conflict with the base connection. Unfortunately I couldn't find any information on how to open a second connection to the same database on the web.
Is this even possible? Or am I just doing this the wrong way?