Long running threads and DB timeout

I have long running thread that is listening a queue (kafka in my case).

My problem is that when the are no task to do and one comes in when db timeout has reached the thread throws a org.hibernate.exception.JDBCConnectionException (or com.mysql.jdbc.exceptions.jdbc4.CommunicationsException in case thet task use plain querys).

How can I force a reconnection ? I have tried ormcloseallsessions in the catch without luck. The code more or less is:

  while(queue.poll()) {
    try { 
      // my task code. 
    } catch(any e) {
          if (e.type=="com.mysql.jdbc.exceptions.jdbc4.CommunicationsException") {
              ormcloseallsessions();
              retry;
          }
    }
  }

Using Lucee 4.5

Thanks

With the help of FR, I can see that the thread get stuck when try to get the datasource Connection, trying to close a connection that I think it doesn’t have anymore:

"cfthread-7" Id=362 RUNNABLE 
   java.lang.Thread.State: RUNNABLE
        at com.mysql.jdbc.ConnectionImpl.close(ConnectionImpl.java:1607)
        - locked com.mysql.jdbc.JDBC4Connection@7496e113
        at com.intergral.fusionreactor.jdbc.ConnectionSurrogate.close(ConnectionSurrogate.java:99)
        at lucee.runtime.db.driver.ConnectionProxy.close(ConnectionProxy.java:137)
        at lucee.commons.io.IOUtil.closeEL(IOUtil.java:493)
        at lucee.runtime.db.DatasourceConnectionPool.getDatasourceConnection(DatasourceConnectionPool.java:65)
        at lucee.runtime.orm.hibernate.CommonUtil.getDatasourceConnection(CommonUtil.java:562)
        at lucee.runtime.orm.hibernate.HibernateORMSession.(HibernateORMSession.java:82)
        at lucee.runtime.orm.hibernate.HibernateORMEngine.createSession(HibernateORMEngine.java:96)
        at lucee.runtime.PageContextImpl.getORMSession(PageContextImpl.java:3161)
        at lucee.runtime.orm.ORMUtil.getSession(ORMUtil.java:59)
        at lucee.runtime.orm.ORMUtil.getSession(ORMUtil.java:55)
        at lucee.runtime.functions.orm.ORMExecuteQuery._call(ORMExecuteQuery.java:58)
        at lucee.runtime.functions.orm.ORMExecuteQuery.call(ORMExecuteQuery.java:42)