title: How to implement EJB timer service in Wildfly AS 8 for a datasource
body: <p>I am migrating the EAR application from JBoss AS 6 to Wildfly AS 8.2.0. While doing, I am facing a concern on implementing <code>EJB timer service</code> in Wildfly AS for the datasource <code>TestSybaseDB</code> defined in <code>standalone-full-ha.xml</code>.</p>
  
  <p>FYI that below are the files related to JBoss AS 6 related to EJB3 timer service implementation for the datasource <code>TestSybaseDB</code>. In below files of JBoss 6 AS, datasource name is defined and I believe these changes are enough for implementation of EJB timer service in JBoss 6 AS for a database.</p>
  
  <p><strong>persistence.xml</strong></p>
  
  <pre><code>path: ${JBOSS_HOME}\common\deploy\jboss-ejb3-timerservice-mk2.jar\META-INF\persistence.xml
  
  &lt;jta-data-source&gt;java:/TestSybaseDB&lt;/jta-data-source&gt;
  </code></pre>
  
  <p><strong>ejb2-timer-service.xml</strong></p>
  
  <pre><code>path: ${JBOSS_HOME}\server\default\deploy\ejb2-timer-service.xml
  
  &lt;depends&gt;jboss.jdbc:datasource=TestSybaseDB,service=metadata&lt;/depends&gt;
  &lt;depends optional-attribute-name="DataSource"&gt;jboss.jca:service=DataSourceBinding,name=TestSybaseDB&lt;/depends&gt;
  </code></pre>
  
  <p><br/>
  But to implement in Wildfly AS, I had converted the content of <code>timer-sql.properties</code> according to Sybase DB. </p>
  
  <p><strong>timer-sql.properties</strong></p>
  
  <pre><code>path: ${JBOSS_HOME}\modules\system\layers\base\org\jboss\as\ejb3\main\timers\timer-sql.properties
  
  create-table=CREATE TABLE JBOSS_EJB_TIMER (ID VARCHAR PRIMARY KEY NOT NULL, TIMED_OBJECT_ID VARCHAR NOT NULL, INITIAL_DATE DATETIME, REPEAT_INTERVAL BIGINT, NEXT_DATE DATETIME, PREVIOUS_RUN DATETIME, PRIMARY_KEY VARCHAR, INFO VARCHAR, TIMER_STATE VARCHAR, SCHEDULE_EXPR_SECOND VARCHAR, SCHEDULE_EXPR_MINUTE VARCHAR, SCHEDULE_EXPR_HOUR VARCHAR,SCHEDULE_EXPR_DAY_OF_WEEK VARCHAR, SCHEDULE_EXPR_DAY_OF_MONTH VARCHAR, SCHEDULE_EXPR_MONTH VARCHAR, SCHEDULE_EXPR_YEAR VARCHAR, SCHEDULE_EXPR_START_DATE VARCHAR, SCHEDULE_EXPR_END_DATE VARCHAR, SCHEDULE_EXPR_TIMEZONE VARCHAR, AUTO_TIMER SMALLINT, TIMEOUT_METHOD_NAME VARCHAR, TIMEOUT_METHOD_DECLARING_CLASS VARCHAR, TIMEOUT_METHOD_DESCRIPTOR VARCHAR, CALENDAR_TIMER SMALLINT, PARTITION_COL VARCHAR NOT NULL);
  create-timer=INSERT INTO JBOSS_EJB_TIMER (ID, TIMED_OBJECT_ID, INITIAL_DATE, REPEAT_INTERVAL, NEXT_DATE, PREVIOUS_RUN, PRIMARY_KEY, INFO, TIMER_STATE, SCHEDULE_EXPR_SECOND, SCHEDULE_EXPR_MINUTE, SCHEDULE_EXPR_HOUR, SCHEDULE_EXPR_DAY_OF_WEEK, SCHEDULE_EXPR_DAY_OF_MONTH, SCHEDULE_EXPR_MONTH, SCHEDULE_EXPR_YEAR, SCHEDULE_EXPR_START_DATE, SCHEDULE_EXPR_END_DATE, SCHEDULE_EXPR_TIMEZONE, AUTO_TIMER, TIMEOUT_METHOD_NAME, TIMEOUT_METHOD_DECLARING_CLASS, TIMEOUT_METHOD_DESCRIPTOR, CALENDAR_TIMER, PARTITION_COL) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25);
  update-timer=UPDATE JBOSS_EJB_TIMER SET NEXT_DATE=$1, PREVIOUS_RUN=$2, TIMER_STATE=$3 WHERE TIMED_OBJECT_ID=$4 and ID=$5 AND PARTITION_COL=$6;
  delete-timer=DELETE FROM JBOSS_EJB_TIMER WHERE TIMED_OBJECT_ID=$1 and ID=$2 AND PARTITION_COL=$3;
  load-all-timers=SELECT ID, TIMED_OBJECT_ID, INITIAL_DATE, REPEAT_INTERVAL, NEXT_DATE, PREVIOUS_RUN, PRIMARY_KEY, INFO, TIMER_STATE, SCHEDULE_EXPR_SECOND, SCHEDULE_EXPR_MINUTE, SCHEDULE_EXPR_HOUR, SCHEDULE_EXPR_DAY_OF_WEEK, SCHEDULE_EXPR_DAY_OF_MONTH, SCHEDULE_EXPR_MONTH, SCHEDULE_EXPR_YEAR, SCHEDULE_EXPR_START_DATE, SCHEDULE_EXPR_END_DATE, SCHEDULE_EXPR_TIMEZONE, AUTO_TIMER, TIMEOUT_METHOD_NAME, TIMEOUT_METHOD_DECLARING_CLASS, TIMEOUT_METHOD_DESCRIPTOR, CALENDAR_TIMER FROM JBOSS_EJB_TIMER WHERE TIMED_OBJECT_ID=$1 AND PARTITION_COL=$2;
  load-timer=SELECT ID, TIMED_OBJECT_ID, INITIAL_DATE, REPEAT_INTERVAL, NEXT_DATE, PREVIOUS_RUN, PRIMARY_KEY, INFO, TIMER_STATE, SCHEDULE_EXPR_SECOND, SCHEDULE_EXPR_MINUTE, SCHEDULE_EXPR_HOUR, SCHEDULE_EXPR_DAY_OF_WEEK, SCHEDULE_EXPR_DAY_OF_MONTH, SCHEDULE_EXPR_MONTH, SCHEDULE_EXPR_YEAR, SCHEDULE_EXPR_START_DATE, SCHEDULE_EXPR_END_DATE, SCHEDULE_EXPR_TIMEZONE, AUTO_TIMER, TIMEOUT_METHOD_NAME, TIMEOUT_METHOD_DECLARING_CLASS, TIMEOUT_METHOD_DESCRIPTOR, CALENDAR_TIMER FROM JBOSS_EJB_TIMER WHERE TIMED_OBJECT_ID=$1 and ID=$2 AND PARTITION_COL=$3;
  create-table.hsql=CREATE TABLE JBOSS_EJB_TIMER (ID VARCHAR PRIMARY KEY NOT NULL, TIMED_OBJECT_ID VARCHAR NOT NULL, INITIAL_DATE DATETIME, REPEAT_INTERVAL BIGINT, NEXT_DATE DATETIME, PREVIOUS_RUN DATETIME, PRIMARY_KEY VARCHAR, INFO VARCHAR, TIMER_STATE VARCHAR, SCHEDULE_EXPR_SECOND VARCHAR, SCHEDULE_EXPR_MINUTE VARCHAR, SCHEDULE_EXPR_HOUR VARCHAR,SCHEDULE_EXPR_DAY_OF_WEEK VARCHAR, SCHEDULE_EXPR_DAY_OF_MONTH VARCHAR, SCHEDULE_EXPR_MONTH VARCHAR, SCHEDULE_EXPR_YEAR VARCHAR, SCHEDULE_EXPR_START_DATE VARCHAR, SCHEDULE_EXPR_END_DATE VARCHAR, SCHEDULE_EXPR_TIMEZONE VARCHAR, AUTO_TIMER SMALLINT, TIMEOUT_METHOD_NAME VARCHAR, TIMEOUT_METHOD_DECLARING_CLASS VARCHAR, TIMEOUT_METHOD_DESCRIPTOR VARCHAR, CALENDAR_TIMER SMALLINT, PARTITION_COL VARCHAR NOT NULL);CREATE INDEX JBOSS_EJB_TIMER_IDENX ON JBOSS_EJB_TIMER (PARTITION_COL, TIMED_OBJECT_ID);
  </code></pre>
  
  <p><strong>Timer service definition in standalone-full-ha.xml</strong></p>
  
  <pre><code>&lt;timer-service thread-pool-name="timer" default-data-store="clustered-store"&gt;
      &lt;data-stores&gt;
          &lt;database-data-store name="clustered-store" datasource-jndi-name="java:/TestSybaseDB" partition="timer"/&gt;
      &lt;/data-stores&gt;
  &lt;/timer-service&gt;
  </code></pre>
  
  <p>I am getting the below error:</p>
  
  <p><strong>Error</strong></p>
  
  <pre><code>ERROR [org.jboss.as.ejb3] (MSC service thread 1-2) JBAS014263: Cannot create table for timer persistence: com.sybase.jdbc4.jdbc.SybSQLException: There is already an object named 'JBOSS_EJB_TIMER' in the database.
  
  at com.sybase.jdbc4.tds.Tds.a(Unknown Source)
  at com.sybase.jdbc4.tds.Tds.nextResult(Unknown Source)
  at com.sybase.jdbc4.jdbc.ResultGetter.nextResult(Unknown Source)
  at com.sybase.jdbc4.jdbc.SybStatement.nextResult(Unknown Source)
  at com.sybase.jdbc4.jdbc.SybStatement.nextResult(Unknown Source)
  at com.sybase.jdbc4.jdbc.SybStatement.updateLoop(Unknown Source)
  at com.sybase.jdbc4.jdbc.SybStatement.executeUpdate(Unknown Source)
  at com.sybase.jdbc4.jdbc.SybStatement.executeUpdate(Unknown Source)
  at org.jboss.jca.adapters.jdbc.WrappedStatement.executeUpdate(WrappedStatement.java:375)
  at org.jboss.as.ejb3.timerservice.persistence.database.DatabaseTimerPersistence.runCreateTable(DatabaseTimerPersistence.java:150) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
  at org.jboss.as.ejb3.timerservice.persistence.database.DatabaseTimerPersistence.start(DatabaseTimerPersistence.java:117) [wildfly-ejb3-8.2.0.Final.jar:8.2.0.Final]
  at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
  at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_67]
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_67]
  at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_67]
  </code></pre>
  
  <p>From the above error, I thought <code>create-table</code> in <code>timer-sql.properties</code> is causing the problem and hence, I tried removing it but getting the below error.</p>
  
  <pre><code>ERROR [org.jboss.msc.service.fail] (MSC service thread 1-8) MSC000001: Failed to start service jboss.ejb3.timerService.timerPersistence.clustered-store: org.jboss.msc.service.StartException in service jboss.ejb3.timerService.timerPersistence.clustered-store: Failed to start service
  at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1904) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [rt.jar:1.7.0_67]
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [rt.jar:1.7.0_67]
  at java.lang.Thread.run(Thread.java:745) [rt.jar:1.7.0_67]
  Caused by: java.lang.NullPointerException
  at org.jboss.as.ejb3.timerservice.persistence.database.DatabaseTimerPersistence.runCreateTable(DatabaseTimerPersistence.java:146)
  at org.jboss.as.ejb3.timerservice.persistence.database.DatabaseTimerPersistence.start(DatabaseTimerPersistence.java:117)
  at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
  at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881) [jboss-msc-1.2.2.Final.jar:1.2.2.Final]
  ... 3 more
  
  ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) JBAS014613: Operation ("add") failed - address: ([
  ("subsystem" =&gt; "ejb3"),
  ("service" =&gt; "timer-service"),
  ("database-data-store" =&gt; "clustered-store")
  ]) - failure description: {"JBAS014671: Failed services" =&gt; {"jboss.ejb3.timerService.timerPersistence.clustered-store" =&gt; "org.jboss.msc.service.StartException in service jboss.ejb3.timerService.timerPersistence.clustered-store: Failed to start service
  Caused by: java.lang.NullPointerException"}}
  
  JBAS014777:   Services which failed to start:      service jboss.ejb3.timerService.timerPersistence.clustered-store: org.jboss.msc.service.StartException in service jboss.ejb3.timerService.timerPersistence.clustered-store: Failed to start service  
  </code></pre>
  
  <p>Could anyone please help me in resolving the error and configure EJB timer service for the datasource defined in Wildfly AS.</p>