The EJB3 Standard provides some annotations to handle general approaches, like transactions (@TransactionAttribute) and security (@RolesAllowed, etc.). To use these methods, you only have to add them to the concerning method.
1 2 3 4 5 6 7 8 | @Stateless @Local(SampleStateless.class) public class SampleStatelessBean implements SampleStateless { @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void sampleMethod() { // do some stuff } } |
Sometimes you have to call methods of other business objects to handle the business logic. For example you have a batch update separated into many small update steps. Here you want to split the main transaction into several small transactions and the main part should have no transaction, because it only handles the reading of external data (The example code is not complete and should only demonstrate the usage of the annotations.).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | @Stateless @Local(SampleStateless.class) public class SampleStatelessBean implements SampleStateless { @TransactionAttribute(TransactionAttributeType.NEVER) public void import() { //read some data while (haveData) { importData(subData); } } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) private void importData(List data) { // do some stuff } } |
This above code is unfortunately NOT CORRECT. The reason is that the @TransactionAttribute-annotations will only be honored, if you call the method via a business interface. So the first solutions is to inject the bean itself and call the submethod via the injected bean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | @Stateless @Local(SampleStateless.class) public class SampleStatelessBean implements SampleStateless { @EJB private SampleStateless bp; @TransactionAttribute(TransactionAttributeType.NEVER) public void import() { //read some data while (haveData) { bp.importData(subData); } } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void importData(List data) { // do some stuff } } |
Another solution, which is quite faster on JBoss in my tests, is to use the SessionContext to get a reference to the current business object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | @Stateless @Local(SampleStateless.class) public class SampleStatelessBean implements SampleStateless { private SampleStateless bp; @Resource private SessionContext ctx; @PostConstruct public void init() { bp = ctx.getBusinessObject(SampleStateless.class); } @TransactionAttribute(TransactionAttributeType.NEVER) public void import() { //read some data while (haveData) { bp.importData(subData); } } @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW) public void importData(List data) { // do some stuff } } |