Jooq, Spring, And BoneCP connection closed twice error

I am using Spring 4.0.0, along with jOOQ 3.2.0 and BoneCP 0.8.0 for a web application.

I have the PersistenceContext configured the same as this guide (please skim read it's a little too much code to paste here)

but with a smaller number of max connections and closeConnectionWatch = true for error checking.

From what I can deduce, this guide is a non-XML version of the jOOQ website's own guide seen here

My problem comes from probably not knowing how to use the jOOQ generated DAOs or the @Transactional annotation. I am coming across loads of "connection closed twice" exceptions, which makes my application fundamentally broken. The following stack trace doesn't actually say it is closed twice, but the output of closeConnectionWatch says something along the lines of

bonecp connection closed twice detected: first location connection was closed in thread[blah]
closed again in thread[blah2]

Stack trace of SQL Exception after the connection watch does its thing:

Jan 28, 2014 10:51:51 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/application] threw     exception [Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException: jOOQ; uncategorized SQLException for SQL 
<snip> error code [0]; Connection is closed!; nested exception is java.sql.SQLException: Connection is closed!] with root cause java.sql.SQLException: Connection is closed!
at com.jolbox.bonecp.ConnectionHandle.checkClosed(
at com.jolbox.bonecp.ConnectionHandle.prepareStatement(
at sun.reflect.GeneratedMethodAccessor40.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy$LazyConnectionInvocationHandler.invoke(
at com.sun.proxy.$Proxy73.prepareStatement(Unknown Source)
at sun.reflect.GeneratedMethodAccessor40.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(
at com.sun.proxy.$Proxy73.prepareStatement(Unknown Source)
at org.jooq.impl.ProviderEnabledConnection.prepareStatement(
at org.jooq.impl.SettingsEnabledConnection.prepareStatement(
at org.jooq.impl.AbstractResultQuery.prepare(
at org.jooq.impl.AbstractQuery.execute(
at org.jooq.impl.AbstractResultQuery.fetch(
at org.jooq.impl.SelectImpl.fetch(
at org.jooq.impl.DAOImpl.fetch(
----> at
at com.myapplication.spring.controllers.LoginController.doLogin(
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(
at org.springframework.web.servlet.DispatcherServlet.doDispatch(
at org.springframework.web.servlet.DispatcherServlet.doService(
at org.springframework.web.servlet.FrameworkServlet.processRequest(
at org.springframework.web.servlet.FrameworkServlet.doPost(
at javax.servlet.http.HttpServlet.service(
at org.springframework.web.servlet.FrameworkServlet.service(
at javax.servlet.http.HttpServlet.service(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at com.github.dandelion.datatables.core.web.filter.DatatablesFilter.doFilter(
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(
at org.apache.catalina.core.ApplicationFilterChain.doFilter(
at org.apache.catalina.core.StandardWrapperValve.invoke(
at org.apache.catalina.core.StandardContextValve.invoke(
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(
at org.apache.catalina.core.StandardHostValve.invoke(
at org.apache.catalina.valves.ErrorReportValve.invoke(
at org.apache.catalina.valves.AccessLogValve.invoke(
at org.apache.catalina.core.StandardEngineValve.invoke(
at org.apache.catalina.connector.CoyoteAdapter.service(
at org.apache.coyote.http11.AbstractHttp11Processor.process(
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$ Source)
at Source)

The line where I have arrowed is the line in the service that makes the call to the database. I have @Autowired DAO objects in the service, as below

public class UserService implements UserServiceInterface{

@Autowired UsersDao userDao;
@Autowired PasswordServiceInterface passwordService;


public Users authenticate(String user,String password) {
    boolean allowedIn = false;

    List<Users> users = userDao.fetch(USERS.USERNAME, user);
            //do something here

Other functions I use in similar services contain calls using the DSLContext object like

The DAO and DSLContext are stored as beans in PersistenceContext like so. I am autowiring as a DSLContext and not a *Default*DSLContext as the jOOQ guide has a test method down the bottom showing only a DSLContext.

public DefaultDSLContext dsl() {
    return new DefaultDSLContext(configuration());

public UsersDao userDao() { //bad because UsersDao isn't an interface???
    return new UsersDao(configuration());

And this is the controller

public class LoginController {

@Autowired UserServiceInterface userService;

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
    return "login";

@RequestMapping(value = "/login/doLogin", method = RequestMethod.POST)
public String doLogin(@RequestParam("username")String username, @RequestParam("password") String password, HttpSession session) {

    Users u = userService.authenticate(username, password);
    if(u == null) 
        return "redirect:/error";
    else {
        session.setAttribute("user", u.getUserid());
        session.setAttribute("role", u.getRoleid());
        session.setAttribute("retailgroup", u.getGroupid());
        return "redirect:/dashboard";

UserService is not the only service I get errors in -- all of my services are similar in that they contain one/both a DAO and a DSLContext Autowired object, with the same DAO configuration constructor as usersDao(), for example productsDao(). The Products service has this DAO and a DSLContext object, and much the same as the UsersService it makes calls to the database.

Sometimes I'll get this connection issue logging in, other times it will be fine and I can browse the site and look at products for a little while, but then randomly I'll get a 'connection is closed!' error coming from another service (there are about 5 that are written in the same way).

So my questions are

  1. Where do I use the @Transactional annotation, and what does it actually do. Does my omission of the @Transactional annotation mean I am causing myself problems? I have previously added it in at all locations that use the DB, but I cant be sure if it was actually helping as I was still getting the same errors.

  2. Is it an issue with my scope for something? I know beans are default as singleton - I've written my controllers in such a way that they use session stored attributes to pass to the services (which are all left as default singletons), so that they may only select data that a certain user is allowed to see.

  3. Since the connectionPool is closing a connection twice, does this mean that the issue is that thread A and thread B go for a connection at the same time, do something with it, and then both close? Why is this happening using the configuration from the above guide? How do I ensure thread safety or is that not the problem?

  4. Are the DAO beans supposed to be interfaces, as from my brief history with Spring I am led to believe a lot(many/all?) @Autowired beans should be? Am I supposed to be using the interface org.jooq.DAOImpl which is the interface that all the jOOQ generated DAOs seem to implement?

    public org.jooq.impl.DAOImpl usersDao() {
        return new usersDao(configuration());

Apologies for the long question, any help would be greatly appreciated. Thanks.

Edit: This is my configuration in PersistenceContext class

public class PersistenceContext {

private Environment env;

@Bean(destroyMethod = "close")
public DataSource dataSource() {
    BoneCPDataSource dataSource = new BoneCPDataSource();
    return dataSource;

public LazyConnectionDataSourceProxy lazyConnectionDataSource() {
    return new LazyConnectionDataSourceProxy(dataSource());

public TransactionAwareDataSourceProxy transactionAwareDataSource() {
    return new TransactionAwareDataSourceProxy(lazyConnectionDataSource());

public DataSourceTransactionManager transactionManager() {
    return new DataSourceTransactionManager(lazyConnectionDataSource());

public DataSourceConnectionProvider connectionProvider() {
    return new DataSourceConnectionProvider(transactionAwareDataSource());

public JOOQToSpringExceptionTransformer jooqToSpringExceptionTransformer() {
    return new JOOQToSpringExceptionTransformer();

public DefaultConfiguration configuration() {
    DefaultConfiguration jooqConfiguration = new DefaultConfiguration();
    jooqConfiguration.set(new DefaultExecuteListenerProvider(

    String sqlDialectName = env.getRequiredProperty("jooq.sql.dialect");
    SQLDialect dialect = SQLDialect.valueOf(sqlDialectName);

    return jooqConfiguration;

public DefaultDSLContext dsl() {
    return new DefaultDSLContext(configuration());

public UsersDao userDao() {
    return new UsersDao(configuration());



After re-reading your question and your chat, I can tell that it is really most likely due to your using version 3.2.0, which had this rather severe bug here:

That bug was fixed in 3.2.2, to which (or later) you should upgrade.

Need Your Help

Prevent Sphinx from executing the module

python python-sphinx

I am trying to do python documentation generation with Sphinx. The problem is that sphinx-build ends up executing the module/evaluating anything in global scope. Is there a reason it does this? And...

Issue while adding attachment to QC Test Case using Com4j API

java qc com4j

I am trying to add attachment to QC Test LAB Test Case run from my Java code using Com4J API. I was able to create a successful run, however while adding attachments below code is throwing invalid