0

We have an web application which uses servlets, jsps and Threads which is a MultiThreaded application. We use JDK1.8, Wildfly server 8.2.I have created a ContextListener as shown below to get the connection object using JNDI datasource. Is this correct way of getting the connection using Connection Pool?

AppContextListener.java

public class AppContextListener implements ServletContextListener
{

  /* (non-Javadoc)    
   * @see javax.servlet.ServletContextListener#contextDestroyed(javax.servlet.ServletContextEvent)    
   */    
  @Override    
  public void contextDestroyed(ServletContextEvent servletContextEvent)
  {        
    ServletContext ctx = servletContextEvent.getServletContext();  
    DBConnectionManager dbManager = (DBConnectionManager)ctx.getAttribute("DBManager");      
    dbManager.closeConnection();  
  }

  /* (non-Javadoc)  
   * @see   javax.servlet.ServletContextListener#contextInitialized(javax.servlet.ServletContextEvent)  
   */  
  @Override  
  public void contextInitialized(ServletContextEvent servletContextEvent)
  {  
    ServletContext ctx = servletContextEvent.getServletContext();  
    String jndiName = ctx.getInitParameter("JNDI");  

    //create database connection from context parameters and set it to context
    DBConnectionManager dbManager = new DBConnectionManager(jndiName);
    ctx.setAttribute("DBManager", dbManager);  
  }
}

DBConnectionManager.java

public class DBConnectionManager
{
  private static Logger logger=Logger.getLogger(DBConnectionManager.class);
  private static Connection conn;
  private String jndiName;

  public DBConnectionManager(String jndiName) {
    this.jndiName = jndiName;
    try {
       Context ctx = new InitialContext();
       DataSource ds = (DataSource) ctx.lookup(this.jndiName);
       conn = ds.getConnection();
       conn.setAutoCommit(false);
    } catch (NamingException | SQLException e) {
       logger.error("[DBConnectionManager.DBConnectionManager]Exception: "+e.getMessage());
    }    
  }

  public static Connection getConnection(){
     return conn; 
  }

  public void closeConnection(){
     if(conn != null){
       try {
          conn.close();
       } catch (SQLException e) {
          logger.error("[DBConnectionManager.closeConnection]Exception: "+e.getMessage());
       }
     }
  }
}

Whenever I need a connection object I call the static method getConnection(). Seems I am not achieving Connection pool using this way. How should I get the connection to achieve Connection pool?

My server will be running for more than 12 hours and When I try to reload the messages sent table system will throw the below exceptions which is causing due to Threads. How can I solve this?

TxConnectionListener:380 - IJ000305: Connection error occured: org.jboss.jca.core.connectionmanager.listener.TxConnectionListener@bed9492[state=NORMAL managed connection=org.jboss.jca.adapters.jdbc.local.LocalManagedConnection@5df6b58b connection handles=1 lastUse=1462985106208 trackByTx=false pool=org.jboss.jca.core.connectionmanager.pool.strategy.OnePool@1299562a mcp=SemaphoreArrayListManagedConnectionPool@3a1d6b71[pool=RDTSDS] xaResource=LocalXAResourceImpl@4b534943[connectionListener=bed9492 connectionManager=f7fbabb warned=false currentXid=null productName=Oracle productVersion=Oracle Database 12c Enterprise Edition Release 12.1.0.2.0 - 64bit Production
With the Partitioning, OLAP, Advanced Analytics and Real Application Testing options jndiName=java:jboss/RDTSDS] txSync=null]
javax.resource.spi.ResourceAdapterInternalException: Unexpected error
    at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.broadcastConnectionError(BaseWrapperManagedConnection.java:644)
    at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.connectionError(BaseWrapperManagedConnection.java:610)
    at org.jboss.jca.adapters.jdbc.WrappedConnection.checkException(WrappedConnection.java:1640)
    at org.jboss.jca.adapters.jdbc.WrappedStatement.checkException(WrappedStatement.java:1267)
    at org.jboss.jca.adapters.jdbc.WrappedPreparedStatement.executeQuery(WrappedPreparedStatement.java:467)
    at com.ctsu.rdts.beans.ReconcileInfo.getNextMessage(ReconcileInfo.java:493)
    at com.ctsu.rdts.beans.GroupInfo.sendMessage(GroupInfo.java:818)
    at com.ctsu.rdts.app.GroupSendThread.send(GroupSendThread.java:114)
    at com.ctsu.rdts.app.GroupSendThread.run(GroupSendThread.java:68)
 Caused by: java.lang.ThreadDeath
    at java.lang.Thread.stop(Thread.java:850)
    at com.ctsu.rdts.app.HttpDequeue.stopThread(HttpDequeue.java:944)
    at com.ctsu.rdts.app.HttpDequeue.clearLocks(HttpDequeue.java:418)
    at com.ctsu.rdts.app.HttpDequeue.run(HttpDequeue.java:404)

Exception in Server log :

WARN  [org.jboss.as.connector.subsystems.datasources.AbstractDataSourceService$WildFlyLocalMCF] (NRG) Queued thread: ECOG-ACRIN: java.lang.Throwable: Queued thread: ECOG-ACRIN
    at sun.misc.Unsafe.park(Native Method) [rt.jar:1.8.0_66]
    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175) [rt.jar:1.8.0_66]
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836) [rt.jar:1.8.0_66]
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireQueued(AbstractQueuedSynchronizer.java:870) [rt.jar:1.8.0_66]
    at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquire(AbstractQueuedSynchronizer.java:1199) [rt.jar:1.8.0_66]
    at java.util.concurrent.locks.ReentrantLock$FairSync.lock(ReentrantLock.java:224) [rt.jar:1.8.0_66]
    at java.util.concurrent.locks.ReentrantLock.lock(ReentrantLock.java:285) [rt.jar:1.8.0_66]
    at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.lock(BaseWrapperManagedConnection.java:373)
    at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.tryLock(BaseWrapperManagedConnection.java:388)
    at org.jboss.jca.adapters.jdbc.WrappedConnection.lock(WrappedConnection.java:147)
    at org.jboss.jca.adapters.jdbc.WrappedConnection.prepareCall(WrappedConnection.java:589)

I am stuck in this issue since 15 days. Can someone help me to resolve this issue?

Usha
  • 194
  • 1
  • 4
  • 17

1 Answers1

0

Check you code in this part HttpDequeue and the thread implementation, you can use this guide to replace the Thread.stop() implementation https://docs.oracle.com/javase/7/docs/technotes/guides/concurrency/threadPrimitiveDeprecation.html

 Caused by: java.lang.ThreadDeath
    at java.lang.Thread.stop(Thread.java:850)
    at com.ctsu.rdts.app.HttpDequeue.stopThread(HttpDequeue.java:944)
    at com.ctsu.rdts.app.HttpDequeue.clearLocks(HttpDequeue.java:418)
    at com.ctsu.rdts.app.HttpDequeue.run(HttpDequeue.java:404)

An instance of ThreadDeath is thrown in the victim thread when the (deprecated) Thread.stop() method is invoked.

An application should catch instances of this class only if it must clean up after being terminated asynchronously. If ThreadDeath is caught by a method, it is important that it be rethrown so that the thread actually dies.

The top-level error handler does not print out a message if ThreadDeath is never caught.

victor sosa
  • 899
  • 13
  • 27