Posted by : Mukunthan GJ Sunday 20 April 2014

Cannot use an EntityTransaction while using JTA



I found this was useful to fix that scenario Cannot use an EntityTransaction while using JTA.

Bean Managed Transactions

In Bean Managed Transactions, Transactions can be managed by handling exceptions at application level. Following are the key points to be considered
  • Start - When to start a transaction in a business method.
  • Sucess - Identify success scenario when a transaction is to be committed.
  • Failed - Identify failure scenario when a transaction is to be rollback.

Example

package com.tutorialspoint.txn.bmt;

import javax.annotation.Resource;
import javax.ejb.Stateless;
import javax.ejb.TransactionManagement;
import javax.ejb.TransactionManagementType;
import javax.transaction.UserTransaction;

@Stateless
@TransactionManagement(value=TransactionManagementType.BEAN)
public class AccountBean implements AccountBeanLocal {

   @Resource
   private UserTransaction userTransaction;

   public void transferFund(Account fromAccount, double fund ,
      Account toAccount) throws Exception{

      try{
         userTransaction.begin();

         confirmAccountDetail(fromAccount);
         withdrawAmount(fromAccount,fund);

         confirmAccountDetail(toAccount);
         depositAmount(toAccount,fund);

         userTransaction.commit();
      }catch (InvalidAccountException exception){
         userTransaction.rollback();
      }catch (InsufficientFundException exception){
         userTransaction.rollback();
      }catch (PaymentException exception){
         userTransaction.rollback();
      }
   }

   private void confirmAccountDetail(Account account)
      throws InvalidAccountException {
   }

   private void withdrawAmount() throws InsufficientFundException {
   }

   private void depositAmount() throws PaymentException{
   }
}

In this example, we made use of UserTransaction interface to mark beginning of transaction usinguserTransaction.begin() method call. We mark completion of transaction by usinguserTransaction.commit() method and if any exception occured during transaction then we rollback the complete transaction using userTransaction.rollback() method call.

Reference : http://www.tutorialspoint.com/ejb/ejb_transactions.htm



<link rel="stylesheet" href="//code.jquery.com/ui/1.11.1/themes/smoothness/jquery-ui.css">

  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.11.1/jquery-ui.js"></script>
  
  <script>
   

  </script>
    <script>
     $(document).ready(function(){
  $("td").dblclick(function(){
   document.getElementById("dialog").innerHTML = $(this).text();
        $( "#dialog" ).dialog();
  });
});        </script>
<body>
<table border="1">
<tr><td>re</td><td >dsf f ds fd 
    g</td></tr>
<tr><td>as</td><td>rt</td></tr>
</table>    
<div id="dialog" title="Basic dialog" style="display:none;" >
  <p>
    </p>
</div>


</body>








{ 2 comments... read them below or Comment }

  1. http://webdesigntutsplus.s3.amazonaws.com/tuts/297_simpleAdmin/SimpleAdmin%20Source%20Files.zip

    ReplyDelete

- Copyright © Get Codes - Powered by Blogger - Designed by Mukunthan GJ -