-
1. Re: SMPC and forcing a transaction to commit
smokingapipe Feb 9, 2007 1:33 AM (in response to smokingapipe)Actually one crazy thing I could do is inject the plain old EntityManager that isn't the SMPC and use that to do the persist, and then I suppose the MDB will see the Entity, because it will be using the same EM. Would that work?
-
2. Re: SMPC and forcing a transaction to commit
smokingapipe Feb 9, 2007 1:49 AM (in response to smokingapipe)Tried that and it didn't work. Obviously even if I'm using the old @PersistenceContext annotation, I'm still bound to use the transaction created by Seam or the container so I can't do a commit() while the method is still running. Hmmm.
-
3. Re: SMPC and forcing a transaction to commit
smokingapipe Feb 9, 2007 2:26 AM (in response to smokingapipe)I just now tried to use an @Observer. It was in the same class as the class that is doing all this. Anyway, the @Observer got triggered but it acted just like a regular function call, in other words, it happened within the method's transaction.
Surely there's some way to do some things AFTER a transaction has committed?
Btw I upgraded to Seam 1.1.1 to see if that would help me. -
4. Re: SMPC and forcing a transaction to commit
smokingapipe Feb 9, 2007 2:58 AM (in response to smokingapipe)And I tried something like this:
@TransactionManagement(TransactionManagementType.BEAN) public class ..... @Resource SessionContext sessionContext; public void beanSend() { final UserTransaction userTransaction = sessionContext.getUserTransaction(); try { userTransaction.begin(); } catch(NotSupportedException nse) { logger.severe("NotSupportedException: " + nse); } catch(SystemException nse) { logger.severe("SystemException: " + nse); } smpc.persist(myEntity); try { userTransaction.commit(); } catch(RollbackException nse) { logger.severe("RollbackException: " + nse); } catch(HeuristicMixedException nse) { logger.severe("HeuristicMixedException: " + nse); } catch(HeuristicRollbackException nse) { logger.severe("HeuristicRollbackException: " + nse); } catch(SystemException nse) { logger.severe("SystemException: " + nse); }
and it still doesn't work. The entity persists just fine but the MDB can't find it in its PersistenceContext.
I'm quite baffled by how to force something to really really commit and really reall flush and whatever else. -
5. Re: SMPC and forcing a transaction to commit
smokingapipe Feb 9, 2007 3:30 AM (in response to smokingapipe)I gave up on it, and put everything that I need into Serializable fields. This is unfortunate that I can't find out how to attach it to an EntityManager from within the MDB, but I've tried a bunch of stuff all evening and nothing works. Any suggestions would be welcome.
-
6. Re: SMPC and forcing a transaction to commit
smokingapipe Feb 9, 2007 5:25 AM (in response to smokingapipe)And just for fun I also tried events.raiseTransactionSuccessEvent() and that just threw a NullPointerException. I would guess that that method would raise an event that happens AFTER the method / transaction is finished, which is what I want to happen, but it's just throwing NullPointerException and I'm not sure why.
-
7. Re: SMPC and forcing a transaction to commit
gavin.king Feb 9, 2007 10:07 AM (in response to smokingapipe)This is very dangerous stuff. Hibernate entities should never be used in two concurrent transactions/persistence contexts. You will need some kind of DTOish thing to work around.
-
8. Re: SMPC and forcing a transaction to commit
gavin.king Feb 9, 2007 10:10 AM (in response to smokingapipe)"SmokingAPipe" wrote:
And just for fun I also tried events.raiseTransactionSuccessEvent() and that just threw a NullPointerException. I would guess that that method would raise an event that happens AFTER the method / transaction is finished, which is what I want to happen, but it's just throwing NullPointerException and I'm not sure why.
You have to install <core:transactionListener/> like in the booking demo. -
9. Re: SMPC and forcing a transaction to commit
smokingapipe Feb 9, 2007 10:45 AM (in response to smokingapipe)Ah it's always the little things that make the big differences. I'll try that.
-
10. Re: SMPC and forcing a transaction to commit
smokingapipe Feb 9, 2007 3:42 PM (in response to smokingapipe)I tried that and IT WORKED! The message-sending method gets called after the transaction has committed successfully and then the message processor can see the entity in the persistence context, as is needed.
My one suggestion would be, in the raiseTransactionSuccessEvent, if the components.xml is not correct, instead of throwing a mysterious NullPointerException, maybe give a little more info, like a message that says "is core:transactionListener in your components.xml file?" I say this because this is a problem that will come up any time a Seam component method sends an entity to a message queue and the message queue needs to attach the entity. That's something that should be a fairly common usage in cases where messages are being used. -
11. Re: SMPC and forcing a transaction to commit
smokingapipe Feb 9, 2007 3:44 PM (in response to smokingapipe)"gavin.king@jboss.com" wrote:
This is very dangerous stuff. Hibernate entities should never be used in two concurrent transactions/persistence contexts. You will need some kind of DTOish thing to work around.
Wait are you saying that even if I use this events trick to send the message AFTER the transaction has committed, that's still dangerous? Because it's working fine right now, but that doesn't mean it's correct or stable, so I'm curious. -
12. Re: SMPC and forcing a transaction to commit
gavin.king Feb 9, 2007 4:20 PM (in response to smokingapipe)I already fixed this in CVS this morning. This is something I usually do when users run into NPEs in the forum ;-)
What you are doing now is fine. There is no chance of concurrency. -
13. Re: SMPC and forcing a transaction to commit
smokingapipe Feb 9, 2007 6:31 PM (in response to smokingapipe)Cool, thanks for letting me know because I would just be guessing otherwise.
Now my final dumb question: What's the right way to re-attach an entity? This entity has been serialized, so all its collections have been lost (they are marked transient). If I go entityManager.merge(myEntity) I think that will not do what I want. .refresh() won't do it either because that is to be used for entities that are currently attached. I could do myEntity = entityManager.find(MyEntity.class, myEntity.getId()). Is that the right or best way to attach something in this case? -
14. Re: SMPC and forcing a transaction to commit
gavin.king Feb 9, 2007 6:57 PM (in response to smokingapipe)merge() is the correct way