Max Korn
Max Korn

Reputation: 275

Using Transaction with CDI Conversation

I'm building an application which uses CDI (Weld Container) and @ConversationScope for the views. I'd need to start a JTA Transaction at the beginning of the Conversation and commit/rollback at the end. So I have coded this:

@Named 
@ConversationScoped
public class ConversationBean implements Serializable {

 private @Inject UserTransaction utx;
 private @Inject Conversation conversation;

 public void startConversation(){
    conversation.begin();
    try {
        utx.begin();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 }
 public void stopConversation(){
    conversation.end();
    try {
        utx.commit();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 
 }

}

However the outcome is that when I begin the conversation/transaction

09:23:33,795 ERROR [org.jboss.as.txn] (http--127.0.0.1-8180-1) JBAS010152: APPLICATION ERROR: transaction still active in request with status 0

And when I issue a commit:

09:23:56,513 ERROR [stderr] (http--127.0.0.1-8180-1) java.lang.IllegalStateException: BaseTransaction.commit - ARJUNA016074: no transaction!

Any idea how could this work ? my environment is JBoss application server 7.1.1 (Weld container). Thanks Linda

Upvotes: 0

Views: 5800

Answers (1)

LightGuard
LightGuard

Reputation: 5378

I think there's a bit of a problem with your thinking here. A conversation spans multiple requests (otherwise you'd use request scope) while a transaction is bound to a thread on the server. Multiple requests are not (necessarily, especially on a web app serving multiple clients) bound to the same thread. You need the transaction around when you're working with the database or other transactional resource. You should probably rethink it a bit.

Upvotes: 2

Related Questions