EJB container manager transaction

S

ShadowMan

Hi all
I have a stateles EJBSession that uses container managed transactions.
This EJB acts as facade for 'persisting' system, so it use an Helper that
invocates a series of DAOs.
If something goes wrong I would like to abort (rollback) transaction.
At moment I can see, this is done from container only when the EJB method
throws an Exception.
Can I do it in one of class used by EJB without relying on Exception
mechanism?!

Thanx
 
G

Grzegorz Trafny

ShadowMan said:
I have a stateles EJBSession that uses container managed transactions.
This EJB acts as facade for 'persisting' system, so it use an Helper that
invocates a series of DAOs.
If something goes wrong I would like to abort (rollback) transaction.
At moment I can see, this is done from container only when the EJB method
throws an Exception.
Can I do it in one of class used by EJB without relying on Exception
mechanism?!

Hi,

In order to rollback container managed transaction you must call
"EJBContext.setRollbackOnly()" [or throw system exception like
"EJBException"].

Naturally by calling "setRollbackOnly" you don't suddenly stop execution
of "transactioned" method. Calling "setRollbackOnly" gives effect after
complete of method and befor end of transaction.

So, if your code will want to execute [time|resource] consuming tasks after
you call setRollbackOnly you should such code precede by test with
method "EJBContext.getRollbackOnly()"

Greetings
GT

[BTW. I apologize for my English :]
 
S

ShadowMan

Grzegorz Trafny said:
In order to rollback container managed transaction you must call
"EJBContext.setRollbackOnly()" [or throw system exception like
"EJBException"].
I tried with system exception and it appears to work, but I've read that
using EJBContext.setRollbackOnly() is safely.
I'm using stateles session EJB and I haven't access to EJBContext
variable...when I use EJBContext in my business method I have no
autocomplete options and when I digit manually I get a compile error.
Naturally by calling "setRollbackOnly" you don't suddenly stop
execution of "transactioned" method. Calling "setRollbackOnly" gives
effect after complete of method and befor end of transaction.
OK...My business method delegates to an Helper wich throws
MyProjectException....I would capture this and call setRollbackOnly..
method "EJBContext.getRollbackOnly()"

EJBContext is a member property or static class?
Can you post me a simple working code...

thanx
 
G

Grzegorz Trafny

ShadowMan said:
I tried with system exception and it appears to work, but I've read that
using EJBContext.setRollbackOnly() is safely.

Naturally ...
I'm using stateles session EJB and I haven't access to EJBContext
variable...when I use EJBContext in my business method I have no
autocomplete options and when I digit manually I get a compile error.

I thought that you know what is it "EJBContext" and how to get it.
These are basis of EJB ... you must know it.

The simplest way to get "EJBContext" is for example:

public class MyBean implements SessionBean {

private SessionContext context;

public void setSessionContext(SessionContext context) {
this.context = context;
}

... the rest of code ...

}

OK...My business method delegates to an Helper wich throws
MyProjectException....I would capture this and call setRollbackOnly..


EJBContext is a member property or static class?
Can you post me a simple working code...

See above sample ... and read more about ejb

Greetings
GT
 
S

ShadowMan

Grzegorz Trafny said:
The simplest way to get "EJBContext" is for example:

public class MyBean implements SessionBean {

private SessionContext context;

public void setSessionContext(SessionContext context) {
this.context = context;
}

... the rest of code ...

}
hmmm....
I've already tried this ....but

1. I don't see any EJBContext reference...rather I see SessionContext (that
is an extension)...

2. in the auto-generated javadoc for this method ....I've read

/**
* This method is called with no transaction context.
*
*/

so...there is something that I miss....I would the container manages
transactions...
 
G

Grzegorz Trafny

ShadowMan said:
I've already tried this ....but

1. I don't see any EJBContext reference...rather I see SessionContext
(that is an extension)...

Did you see quotations? I assumed that you will understand
metaphore at once :)
2. in the auto-generated javadoc for this method ....I've read

/**
* This method is called with no transaction context.
*
*/

so...there is something that I miss....I would the container manages
transactions...


Naturally, some methods aren't called by container in "full context"
(including transaction context), especially when container begins
creation of ejb object. But this matter DO NOT influence setting class
variable "context" and futher taking advantage of this variable when
ejb object is completely initialized. So I don't see any problem.


Greetings
GT
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top