ejbCreate method and transaction

Discussions

EJB programming & troubleshooting: ejbCreate method and transaction

  1. ejbCreate method and transaction (3 messages)

    Hi,
    This is the code sample in my application.

    //create the bean
    public create(1,2,3,4) throws ...

    //business method
    public double findSalaryForEmployee( id eid );

    Now the question is ,
    How the create and findSalaryForEmployee will be executed. Will the container create a seperate transaction for each create method. How can I configure to include my business methods in the same transaction as that of create method?

    Threaded Messages (3)

  2. ejbCreate method and transaction[ Go to top ]

    If you want the create method and the business methods to run in the same context, then the caller of these methods must have it's own transaction context. If it does, simply use TX_REQUIRED, TX_MANDATORY or TX_SUPPORTED (usually REQUIRED is appropriate) for all the methods involved (create, business).

    Gal
  3. ejbCreate method and transaction[ Go to top ]

    I'd also throw in that you should only use a transaction for something like findSalaryforEmployee during a salary update (hopefully a raise :). If the data is simply being read for a report, do a standard SQL read and avoid the overhead of the transaction. While it's true data could change, it's unlikely (salary data doesn't update that often), even if it does, depending upon context, it may not matter. For example if a manager is simply reviewing salaries, they can just do another query if they want to see any updates. But when you're printing payroll checks, you want to make sure it's correct.

    Adds some complexity to your application but it's wise to avoid transactions whenever possible.

    Mark
  4. ejbCreate method and transaction[ Go to top ]

    Mark,
    I have two notes about your statement:
    1. Even when doing normal SQL read, even directly through JDBC, most DB vendors will run your statements in a transaction anyway.
    2. Not using a transaction for methods in EJB can cause problems with all sorts of things. Caching is one area which is greatly affected by having no valid transaction context. Most users shouldn't attempt it. IMHO, the optimization achieved by such an aproach will be either non-existent or irrelevant in 95% of the cases.

    Gal