Question

I'm developing a JSF program on a Glassfish Server with a MySQL database. I currently have a Singleton LocalBean with a simplified version of the code looking like this:

@Resource
private UserTransaction trans;

@Schedule(second = "30", minute = "*", hour = "*", dayOfWeek = "*", dayOfMonth = "*", month = "*", year = "*", info = "subTimer")
private void scheduledTimeout(final Timer t)
{
        InsertIntoDatabase();
    }

private void InsertIntoDatabase()
{
    trans.begin();
    trans.doTransaction();
    trans.commit(); 
}

Without the schedule, the code would work fine. Unfortunately, I need to run a check and possibly insert information into the MySQL database daily, or possibly two times a day even. I'm getting an error stating this:

 SEVERE: Transaction commit error
 java.lang.IllegalStateException: Operation not allowed.    
 at com.sun.enterprise.transaction.UserTransactionImpl.checkUserTransactionMethodAccess(UserTransactionImpl.java:146)

I tried researching it some and I see that you can't run UserTransactions within a timer or scheduled event, but I need some way of executing a transaction on a schedule and I'm not sure how to proceed with it.

Was it helpful?

Solution

Sorry, stupid question and got it figured out. Since the transaction is being started in the Scheduler, I simply didn't need to have the usertransaction in there as well. So just calling EntityManager.merge() got the job done with all the transaction management being handled by the scheduler it looks like.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top