0

Getting exception when i have already supplied: @TransactionManagement(javax.ejb.TransactionManagementType.BEAN)

Kindly help in fixing error

insertNewRecord:java.lang.IllegalStateException: WFLYEJB0137: Only session and message-driven beans with bean-managed transaction demarcation are allowed to access UserTransaction

Scheduler:

@Startup
@Singleton
@AccessTimeout(value = 30, unit = TimeUnit.MINUTES)
public class SnowPollerNew {

   @EJB
    Scrutiny scrutiny;

   @Schedule(hour = "*", minute = "*/1", persistent = false)
    @TransactionTimeout(value = 2, unit = TimeUnit.HOURS)
    public v

oid runTriggerFetchComputers() {
        File trigFileSNOWApplications = new File(TRIGFILE_DIR + TRIGFILE_COMPUTERS);
        if (trigFileSNOWApplications.exists()) {


            JobStatus jobStatus = scrutiny.insertNewRecord(JobName.COMPUTERS);

Scrutiny:

@Stateless

public class Scrutiny {
    private static final Logger log = Logger.getLogger(Scrutiny.class.getName());

    public Scrutiny() {
        System.out.println("Scrutiny");
    }

    @Inject
    StatusDao statusDao;

    public JobStatus insertNewRecord(JobName jName)  {

        JobStatus js = new JobStatus(jName, new Date());
        js.setStatus(Status.PROGRESS);
        try {
            statusDao.begin();
            statusDao.create(js);
            statusDao.flush();
            statusDao.commit();;

Job Impl:

@TransactionManagement(javax.ejb.TransactionManagementType.BEAN)
public class JobStatusDaoImpl extends GenericDaoImpl<JobStatus, String> implements StatusDao {
    private static final Logger LOG = Logger.getLogger(JobStatusDaoImpl.class.getName());

    @Override
    public List<JobStatus> checkExistingRecordToday(JobName jName) {
        LOG.info("checkExistingRecordToday:" + jName);
        return em.createNamedQuery("findByJobNameAndCurrentDate", JobStatus.class)
            .setParameter("jobName",jName).getResultList();
    }


}

Generic Dao Impl:

public abstract class GenericDaoImpl<T, PK> implements GenericDao<T, PK> {

    private static final Logger LOG = Logger.getLogger(GenericDaoImpl.class.getName());
    @PersistenceContext(unitName = "IntegratorMasterdataDS")
    protected EntityManager em;

    @Resource
    private SessionContext sessionContext;

   @Override
    public void begin() throws IllegalStateException, NotSupportedException, SystemException {
        sessionContext.getUserTransaction().begin();
    }
fatherazrael
  • 5,511
  • 16
  • 71
  • 155
  • Have you tried moving `@TransactionManagement(javax.ejb.TransactionManagementType.BEAN)` to the session bean? I guess that's where it needs the annotation, not on the injected DAO. – Andreas Dolk Feb 24 '20 at 14:27
  • Now trying it on -> @Stateless @TransactionManagement(javax.ejb.TransactionManagementType.BEAN) public class Scrutiny { – fatherazrael Feb 25 '20 at 03:52
  • @Andreas_D: It is saying transaction is required to perform this opeartion where i have already added begin and commit – fatherazrael Feb 25 '20 at 05:02
  • I think you're getting closer, the annotation belongs to the session bean class. But it's hard solve that from a distance. There is a chance that your data source does not support transactions yet. check if you have the `` entry in your persistence xml. – Andreas Dolk Feb 25 '20 at 06:32
  • @Andreas_D Yes data source is there-> java:/datasources/IfsDS ; Actually in my use case, i need to fetch out one operation out of JTA (mentioned in persistance). Status table which insert data on start and end of job. Earlier it was part of single transaction. So i need to introduce UserTransaction. It is wildlfly 16 – fatherazrael Feb 25 '20 at 06:38
  • @Andreas_D: in this example -> https://examples.javacodegeeks.com/enterprise-java/ejb3/transactions/bean-managed-transactions/ (I can see bean managed mentioned on class where At-PersistanceContext is present; but in my case it is abstract and child JObstatusDao is stateless now. – fatherazrael Feb 25 '20 at 06:44

0 Answers0