aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes eplicitly begin and commit Transaction in CMP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » EJB and other Java EE Technologies
Bookmark "eplicitly begin and commit Transaction in CMP" Watch "eplicitly begin and commit Transaction in CMP" New topic
Author

eplicitly begin and commit Transaction in CMP

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I use CMP and want to set the Transaction (commit) within a method explicitly.

I have a method persisting 500 records in ONE Transaction.



I want to commit the transaction after 10 records (!) are inserted.

How could I do it? With UserTransaction? I have no clue. But this does not work. I have only ONE Transaction, but I want that a Transaction should be commited after 10 records are inserted and begins a new Transaction with the next 10 records..


nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have also tried it with Hibernates:



What is wrong, why can I not split my Transactions??
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Is the method that calls this code already in a transaction?


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
yes, the transaction is by default container-managed (@TransactionAttribute(TransactionAttributeType.REQUIRED))

But in this special case, I want to manage the Transaction-Cycle by my own. So I tried it with UserTransaction or HibernateTransaction, without success.
Chris Hurst
Ranch Hand

Joined: Oct 26, 2003
Posts: 420
    
    2

I think the example you want is covered in ...

Enterprise Java Beans 3.0 pg 408 ..

Your probably off getting hold of a copy of the book but some quick notes ..

Their bean is annotated NOT_SUPPORTED for transactions , they inject an extended persistence context / entity manager, they do all their db work in a normal (NOT_SUPPORTED) method (as much as you like uncommitted) and then have another method like the one below ...

I don't want to post the listing as its from a book but ...






The entity(s) are committed to the database when the extended persistence context is enlisted in a transaction when the commitToDB method is called, the flush isn't needed as the context joins the transaction when the method is called.


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I do not get you. You mean, I should it change to @TransactionAttribute(REQUIRED) ??

Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

nimo frey wrote:yes, the transaction is by default container-managed (@TransactionAttribute(TransactionAttributeType.REQUIRED))

But in this special case, I want to manage the Transaction-Cycle by my own. So I tried it with UserTransaction or HibernateTransaction, without success.

So when you call this method you start a new transaction (or join one if it already exists). Which means all transaction behaviour will be governed by the enclosing transaction - your transaction cannot commit until this does.

If you wan't to manage the transactional behaviour yourself you cannot also use CMT. Use NOT_SUPPORTED as Chris suggests.
nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
Okay, I have tried it with that:




but this:


returns always false, so I guess, there is only ONE Transaction.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: eplicitly begin and commit Transaction in CMP