Suggested Pages

Thursday, June 14, 2012

EJB : Transaction Rollback

As you know an enterprise java bean is transactional and you can declare a method to be transactional using specific annotations.
By default a transaction rolls back if an enterprise method throws an unchecked exception and a transaction commits if it throws a checked exception.
You can change this behaviour using @ApplicationException(rollback=true) at class level. Annotating a checked exeption with the above annotation, you can also perform a rollback in case of checked exception thrown by an enterprise method.
In the following snippets, you can see a Steteless Session bean that has three methods:
  • The method saveUncheckedException throws a Runtime exception so the transaction will roll back.
  • The method saveCheckedExceptionRollback throws a checked exception (CommitEception) so the transaction will commit.
  • The method saveCheckedExceptionCommit throws a checked exception (RollbackException) marked with @ApplicationException(rollback=true) so the transaction will roll back.

ServiceContactEJBRemote.java

package ejb.transaction.service;

import ejb.transaction.entity.Contact;
import ejb.transaction.exceptions.CommitException;
import ejb.transaction.exceptions.RollbackException;

@Remote
public interface ServiceContactEJBRemote {

 public void saveUncheckedException (Contact contact);
 
 public void saveCheckedExceptionCommit (Contact contact) throws CommitException;
 
 public void saveCheckedExceptionRollback (Contact contact) throws RollbackException;


ServiceContactEJB.java

package ejb.transaction.service;

...

@Stateless
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ServiceContactEJB implements ServiceContactEJBRemote {

 @PersistenceContext(unitName = "testUnit")
 private EntityManager entityManager;

 @TransactionAttribute(TransactionAttributeType.REQUIRED)
 public void saveUncheckedException(Contact contact) {
  entityManager.persist(contact);
  throw new RuntimeException();
 }

 @TransactionAttribute(TransactionAttributeType.REQUIRED)
 public void saveCheckedExceptionCommit(Contact contact)
   throws CommitException {
  entityManager.persist(contact);
  throw new CommitException();
 }

 @TransactionAttribute(TransactionAttributeType.REQUIRED)
 public void saveCheckedExceptionRollback(Contact contact)
   throws RollbackException {
  entityManager.persist(contact);
  throw new RollbackException();
 }
}


CommitException.java

package ejb.transaction.exceptions;

public class CommitException extends Exception {

 private static final long serialVersionUID = 1L;

 public CommitException() {
  super();
  
 }

 public CommitException(String message, Throwable cause) {
  super(message, cause);

 }

 public CommitException(String message) {
  super(message);
 
 }

 public CommitException(Throwable cause) {
  super(cause);
  
 }

}


RollbackException.java

package ejb.transaction.exceptions;

import javax.ejb.ApplicationException;

@ApplicationException(rollback=true)
public class RollbackException extends Exception{

 public RollbackException() {
  super();
 
 }

 public RollbackException(String message, Throwable cause) {
  super(message, cause);
  
 }

 public RollbackException(String message) {
  super(message);
 
 }

 public RollbackException(Throwable cause) {
  super(cause);
 
 }

 private static final long serialVersionUID = 1L;

}




Contact.java

package ejb.transaction.entity;

...

@Entity
@Table
public class Contact implements Serializable{

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private int id;
 
 @Column
 private String number;
 
 @Column
 private String type;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public String getNumber() {
  return number;
 }

 public void setNumber(String number) {
  this.number = number;
 }

 public String getType() {
  return type;
 }

 public void setType(String type) {
  this.type = type;
 }
}

2 comments :

  1. very excellent blog u give us .u give a very clear explaination of transaction rollback by using java.very exiting to look this block
    java training in chennai |
    java training institutes in chennai |
    java j2ee training institutes in velachery

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete

Suggested Pages