Quartz scheduled jobs could not access datasource in Websphere

别来无恙 提交于 2021-02-10 13:15:34

问题


I am developing a web app where batch programs need to run for specific times. I used Quartz library to schedule the jobs. The web app is deployed on Websphere 8.5.5 and its working fine, accessing the tables through datasources (Datasource given in code is java:comp/env/jdbc/db_datasource). The job is also triggered at the mentioned times.

I am getting an error when the scheduled job makes a DB connection through the datasource and the error is:

javax.naming.ConfigurationException: A JNDI operation on a "java:" name cannot be completed because the server runtime is not able to associate the operation's thread with any J2EE application component.  This condition can occur when the JNDI client using the "java:" name is not executed on the thread of a server application request.  Make sure that a J2EE application does not execute JNDI operations on "java:" names within static code blocks or in threads created by that J2EE application.  Such code does not necessarily run on the thread of a server application request and therefore is not supported by JNDI operations on "java:" names. [Root exception is javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".]
at com.ibm.ws.naming.java.javaURLContextImpl.throwExceptionIfDefaultJavaNS(javaURLContextImpl.java:522)
at com.ibm.ws.naming.java.javaURLContextImpl.throwConfigurationExceptionWithDefaultJavaNS(javaURLContextImpl.java:552)
at com.ibm.ws.naming.java.javaURLContextImpl.lookupExt(javaURLContextImpl.java:481)
at com.ibm.ws.naming.java.javaURLContextRoot.lookupExt(javaURLContextRoot.java:485)
at com.ibm.ws.naming.java.javaURLContextRoot.lookup(javaURLContextRoot.java:370)

I understand from the error message is that the job is running outside the J2ee container and so the datasource is not available for the Job to make the connection, which I cannot agree as the Quartz is implemented as the ServletContextListener and the same is mentioned in web.xml.

Web.xml

<listener>
    <listener-class>com.ehacampaign.helper.EHAJobSchedulerListener</listener-class>
</listener>

EHAJobSchedulerListener.java

public class EHAJobSchedulerListener implements ServletContextListener {..}

As you can see the code, the class is registered in the web and I do not understand why it cannot use the datasource in the J2EE container.

Questions are:

  1. Why servlet registered class cannot access the datasource in J2EE container?
  2. If datasource in container cannot be used, then how to make a connection to the DB while executing the job?

NOTE: I have the same setup in JBoss AS 7.1 and the jobs are running smoothly accessing the datasource configured in JBoss AS 7.1. I have to develop this in Websphere as the customer demands it.

UPDATED

I have attached the modified quartz property file. Even after adding the workmanagerthread, I am getting the same error.

org.quartz.threadPool.threadCount=1
org.quartz.threadPool.class=org.quartz.simpl.SimpleThreadPool

org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
org.quartz.threadExecutor.class=org.quartz.commonj.WorkManagerThreadExecutor
org.quartz.threadExecutor.workManagerName=wm/default

回答1:


In order to perform JNDI lookups in WebSpehre, your code must be running on a managed thread. In order to have Quartz run on one of WebSphere's managed threads, you must set the following 2 properties in your quartz.properties (as Alasdair mentioned in the comments):

org.quartz.threadExecutor.class=org.quartz.commonj.WorkManagerThreadExecutor
org.quartz.threadExecutor.workManagerName=wm/default

The name for org.quartz.threadExecutor.workManagerName can be the JNDI name of any Work Manager that you have configured in WebSphere. I recommend simply using wm/default because it is in your configuration by default.




回答2:


With all the help provided by aguibert and Alasdair and reference from here, I am able to fix the issue.

The Quartz property file is:

org.quartz.threadPool.threadCount=1
org.quartz.jobStore.class=org.quartz.simpl.RAMJobStore
org.quartz.threadExecutor.class=org.quartz.commonj.WorkManagerThreadExecutor
org.quartz.threadExecutor.workManagerName=wm/default

The database connection or JNDI lookup should happen within the empty constructor of the JOB Implemented class. For ex,

public class ContractIdFromPartyServiceJob implements Job {

private DataSource ds;

public ContractIdFromPartyServiceJob() {
    try {
        Logger.info("Gets the data source");
        Context context = new InitialContext();
        ds = (DataSource) context.lookup(ApplicationConstants.RESOURCE_REFERENCE_JDBC);
    } catch (RException e) {
        e.printStackTrace();
    }
}

@Override
public void execute(JobExecutionContext arg0) throws JobExecutionException
{
    EHAMarsDAO marsDao = new EHAMarsDAO();
    Connection con = getConnection();
    try {
      marsDao.callDBMethod(con);
    } finally {
      con.close();
    }
 }

public Connection getConnection() throws RACVException
{
    Connection con = null;

    try {
        con = ds.getConnection();
        con.setAutoCommit(false);
        con.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
    } catch (SQLException e) {
        throw new RException(Constants.ERROR_CODE_002, Constants.E012_DB_CONNECTION_ERROR, e);
    }
    return con;
}
}


来源:https://stackoverflow.com/questions/37871237/quartz-scheduled-jobs-could-not-access-datasource-in-websphere

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!