4 Aralık 2017 Pazartesi

Jakarta EE @TransactionManagement Anotasyonu

Giriş
Şu satırı dahil ederiz
import jakarta.ejb.TransactionManagement;
1. Container Managed Transaction
2. Bean Managed Transaction için kullanılabilir

1. Container Managed Transaction
Örnek
Şöyle yaparız.
@Stateless
@TransactionManagement(value = TransactionManagementType.CONTAINER)
@TransactionAttribute(value = TransactionAttributeType.REQUIRED)
class Remote implements IRemote { 

  void method(){..}

}
2. Bean Managed Transaction
Burada iki kullanım seçeneği var
1. DataSource'tan java.sql.Connection alınır ve transaction bu nesne ile yönetilir.
2. Container'dan javax.transaction.UserTransaction alınır ve transaction bu nesne ile yönetilir.
UserTransaction  nesnesini almak için CDI kullanıyorsak şöyle yaparız
@Inject
private UserTransaction userTransaction;
Eğer EJB kullanıyorsak şöyle yaparız
@Resource
private UserTransaction userTransaction;
Rollback İşlemi
Açıklaması şöyle
... either the UserTransaction ‘s rollback() or setRollbackOnly() methods are used to explicitly rollback the transaction. 

So what is the difference between UserTransaction ‘s rollback() or setRollbackOnly() ? The use of the rollback() method results in the immediate rollback of the transaction. On the other hand, using the setRollbackOnly() method only marks the transaction for rollback. It will not be rolled back until the transaction actually ends. Delaying the rollback can be advantageous since it permits other activities to be performed, such as logging the error conditions.
Örnek - rollback()
Şöyle yaparız
@Stateless
@TransactionManagement(TransactionManagementType.BEAN)
public class ManagedComponent {
  //Ask the container to inject an Entity Manager (EM). 
  //As a consequence the EM will be automatically enlisted into any new
  //bean managed transaction
  @PersistenceContext
  private EntityManager entityManager;
  
  // Inject a UserTransaction for manual transaction demarcation.
  @Inject
  private UserTransaction userTransaction;
  
  public String executeTransaction() {
    try {
      userTransaction.begin();
      // Execute Bean Managed Transaction here
      ...
      userTransaction.commit();
      return result;
    } catch (Exception e) {
      return e.getMessage();
    } finally {
      //Clean up
      try {
        //If transaction is still active it means there was an exception
        if (userTransaction.getStatus() == Status.STATUS_ACTIVE)
          userTransaction.rollback();
        } catch (Throwable e) {
          // ignore
        }
      }
    }
  }
}
Örnek - Message Driven Bean
Şu satırı dahil ederiz
import javax.annotation.Resource;
import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.ejb.MessageDrivenContext;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import javax.transaction.UserTransaction;
import org.jboss.ejb3.annotation.ResourceAdapter;
Şöyle yaparız. Bu sefer UserTransaction  nesnesini direkt enjekte edemiyoruz. Onun yerine MessageDrivenContext enjekte ediliyor ve UserTransaction  bu nesne üzerinden elde ediliyor.
@MessageDriven(name = "MDB_BMTExample", 
  activationConfig = { 
    @ActivationConfigProperty(propertyName = "destinationType",
                              propertyValue = "javax.jms.Queue"),
    @ActivationConfigProperty(propertyName = "destination", 
                              propertyValue = "queue/testQueue"),
    @ActivationConfigProperty(propertyName = "acknowledgeMode", 
                              propertyValue = "Dups-ok-acknowledge") 
  }
)
@TransactionManagement(value = TransactionManagementType.BEAN)
@ResourceAdapter("hornetq-ra.rar")
public class MDB_BMTExample implements MessageListener {
  @Resource
  MessageDrivenContext ctx;
  public void onMessage(Message message) {
    try {
      ...
      // lets look at the user transaction to make sure there isn't one.
      UserTransaction tx = ctx.getUserTransaction();
      if (tx != null) {
        tx.begin();
        ...
        tx.commit();
      } else {
        ...
      }
    } catch (Exception e) {
      ...
    }
  }
}

Hiç yorum yok:

Yorum Gönder