File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

eplicitly begin and commit Transaction in CMP

 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have also tried it with Hibernates:



What is wrong, why can I not split my Transactions??
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the method that calls this code already in a transaction?
 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 443
3
C++ Eclipse IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
nimo frey
Ranch Hand
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do not get you. You mean, I should it change to @TransactionAttribute(REQUIRED) ??

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 580
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I have tried it with that:




but this:


returns always false, so I guess, there is only ONE Transaction.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic