aspose file tools*
The moose likes EJB and other Java EE Technologies and the fly likes Allow single transaction only 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 "Allow single transaction only" Watch "Allow single transaction only" New topic
Author

Allow single transaction only

Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
Hi all
As I understand.. When public stateless Ejb method is called it will open its own transaction,
If this method will call other method on Dao/ ejb annotated with

New transaction will open.

How can I be sure that no other transaction will open,
Is there anyway I can restrict other methods to use same transaction?

The usecase:
I am writing abstract class that has "execute" method, this class will be implemented by other programmers.
Data written to the DB under "execute" method should be removed on error.

Thank you very much
Sharon
[ February 25, 2008: Message edited by: Sharon whipple ]
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
The EJB specification defines the transaction types which the EJB container must implement. If you declare a RequiresNew transaction type, then you just leave it to the container to create a new transaction and, if necessary, to suspend the incoming transaction.

Also, the container is required by the specification to allow only one thread to execute an EJB instance at any time.

So, EJB transactions can be a lot easier to code than you may have thought.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
Think i got it, this is a Jboss expert explanation:
First transaction open, then nested transaction open, (using REQUIRED_NEW)
commit will occur only when first & second transaction end successfully.

In case first transaction fail, it will rollback the nested one.
i will test this behavior and post results.

Sharon
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Be aware that EJBs are not allowed by the EJB specification to support nested transactions. Only flat transactions are allowed. By prohibiting nested transactions, vendors of existing transaction processing and database management systems can incorporate support for EJBs.

In the scenario which you are describing, any existing transaction would be suspended and a new one created.
Gladwin Burboz
Greenhorn

Joined: Feb 26, 2008
Posts: 25
Your question is not very clear. I will make assumption that you are not using EJB's and try to answer to my best.

Instead of 1 method have 4 methods viz. execute(), String getQuery(), void getParameterValues(), void processResultSet()

Make getQuery(), getParameterValues() and processResultSet(ResultSet) as protected abstract methods. Document them clearly todo following:


Make execute() as public final method and implement it to co-ordinate execution of above methods as follows:


Read more on implementing DAO/DAC framework for more in depth understanding


<a href="http://www.sealordresortarnala.com" target="_blank" rel="nofollow">http://www.sealordresortarnala.com</a>
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
Thank you for your reply Roger,
Gladwin your assumption is wrong, i wrote in the original message "stateless Ejb method is called"

I am using Ejb, and thanks to Roger found this in the Spec :
The Enterprise JavaBeans architecture supports flat transactions.
A flat transaction cannot have any child (nested) transactions.

Note:
The decision not to support nested transactions allows vendors of existing transaction
processing and database management systems to incorporate support for Enterprise Java-
Beans. If these vendors provide support for nested transactions in the future, Enterprise Java-
Beans may be enhanced to take advantage of nested transactions.


Is there any other way i can prevent other calls from opening new transaction?

Thank you
Shaorn
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
Is there any other way i can prevent other calls from opening new transaction?

Either you need transactions or you don't. If you need transactions and want to guarantee that an EJB will run in a transaction, then you must use the Requires, RequiresNew or Mandatory attribute.

If you do not need transactions, then you have a choice of NotSupported or Never. They both guarantee that the EJB method will never take part in a transaction. The difference is that NotSupported ensures that if the caller is part of a transaction, then the caller's transaction is suspended. If the EJB method fails, there will be no effect on the caller's transaction, and no rollback will occur. A typical use for NotSupported would be for logging, as you will typically want logging to be done even if the caller's transaction rolls back.

As a general rule, first consider Requires or NotSupported, then ask yourself if any of the other transaction attributes are more appropriate.
Sharon whipple
Ranch Hand

Joined: Jul 31, 2003
Posts: 294
Either you need transactions or you don't.


I don't think I explained the problem properly, I will try to explain on more concrete example,

I am writing Ejb class TaskContainer, class TaskContainer got a public method "doExecute" (transactional method) that will load task (like DataIntegrityCheck) and execute it.

Because I don�t write the DataIntegrityCheck class I want to be sure that it won't commit information by calling "anotherEjb.writeData()"

Bottom line is that TaskContainer.execute( ) mustn�t change the DB data in case of error.

Here is the pseudo code:



DataIntegrityCheck :


AnotherEjb:

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Allow single transaction only