users@glassfish.java.net

Removing stateful EJB and transactions

From: <glassfish_at_javadesktop.org>
Date: Mon, 29 Oct 2007 06:46:05 PST

Hi,

I have a question regarding Stateful EJBs and Transactions. I have the following constellation:

1) I have a test-client calling a stateful session bean.
2) The bean calls another stateful bean.
3) This bean calls a third bean.

However, the bean in step 2 is within another application. The transaction start on step 1, goes to 2 and 3. However, it gets more complicated. The call in step one runs over a InitialContext.lookup method, and in the same method, the "remove" method is called on this bean. This causes the transaction to abort. The question is, if I cannot "remove" the EJB which I call, where does it stay and how long? Will the server passivate it, and how long?

Anyway, here is my code:

[b]Test.java[/b]

... lookup code ...
structureSession.update(persistStructure2.getGuid(), PersistenceMode.OVERWRITE);
structureSession.remove();


[b]StructureSession.java[/b] (Stateful, in application 1)

@TransactionAttribute(TransactionAttributeType.REQUIRED)
public Structure update(String structureGuid, PersistenceMode persistenceMode) {

       // ... business code ....
        
        CollectorSessionRemote x = null;
        try {
            InitialContext i = new InitialContext();
            x = (CollectorSessionRemote) i.lookup(contentCollectorEntity.getJndiName());
        } catch (NamingException ex) {
            ex.printStackTrace();
        }
        

        x.init(); // call init

        x.process(structureEntity.getGuid()); // do processing

        structureEntity = this.findStructureEntityByGuid(userEntity, structureEntity.getGuid()); // when i place the x.remove() method before this call, then I get an explicit transaction error.

        x.remove(); // causes the transaction to fail, without error-msg however when it is behind the this.findStructureEntityByGuid call.
        
        // .... finish ...
    }


[b]CollectorSessionBean.java[/b] (Stateful, in application 2)

   public void init() {
      structureSession = lookup.getStructureSession();
    }
    
    @TransactionAttribute(TransactionAttributeType.REQUIRED)
    public boolean process(final String structureGuid) {
        try {
            Structure structure = structureSession.loadStructureByGuid(structureGuid);

    // This goes on and we even call a third bean. But you get the picture, I hope.

   }

 @Remove()
    public void remove() {
        structureSession.remove();
        System.out.println("Removing: CollectorSessionBean");
    }
    
     
So, when I call the remove method within [b]StructureSession.java[/b], then the remove method in [b]CollectorSessionBean.java[/b] in called, but on return, the transaction FAILS. No data is written in the database. When I remove the "remove" method, then everything functions, but where are the EJBs from my call? When will they be closed? I would feel better if I could remove the called EJB from the method "process" myself. This would save resources, I suppose. And, is there a workaround with doing the transaction-management myself?

Greetings,
Jan De Cooman
[Message sent by forum member 'ossaert' (ossaert)]

http://forums.java.net/jive/thread.jspa?messageID=242692