• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Allow single transaction only

 
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sharon whipple
Ranch Hand
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1683
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 294
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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:

 
money grubbing section goes here:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic