[jboss-user] [Beginners Corner] - Problems getting transaction isolation to work as expected?

kent.narling do-not-reply at jboss.com
Fri May 18 02:10:03 EDT 2007


I have med a very simple test project with simulated bank accounts and transaction (very similar to the example scenario in the transactions chapter in jboss4guide).

However, I have problems getting the transaction isolation as expected.
Since I can very easily get my client to break both the isolation (dirty read/write) and the consistency (total balance of accounts change)!

Here is the transfer() method in my session bean:

      @TransactionAttribute(TransactionAttributeType.REQUIRED)
      public Transaction transfer(
                  String from_account_id, 
                  String to_account_id, 
                  long    amount,
                  String  reference, 
                  String  comment, 
                  long    delay)
      {
            Account from_account = getAccount(from_account_id);
            Account to_account = getAccount(to_account_id);
            from_account.setBalance(from_account.getBalance() - amount);
            to_account.setBalance(to_account.getBalance() + amount);
            Transaction tr = new Transaction(from_account, to_account, amount, reference, comment);

            try 
            {
                  Thread.currentThread().sleep(delay);
            } catch (InterruptedException e) 
            {
                  e.printStackTrace();
            }

            em.persist(tr);
            em.persist(from_account);
            em.persist(to_account);
            
            return tr;
      }

The sleep() I have done just to easily be able to create two overlapping transactions when I test.
I simply open two web windows towards the JSP client and run two overlapping transfers (with eg 10 seconds delay) and both go through with the result that the total balance of the accounts increase!

I must have missed something trivial? can the transaction handling be turned off by default?

I run MySQL 5.0, JBoss AS 4.0.5 GA (with default settings for EJB3) and all the tables are InnoDB tables.




View the original post : http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4046708#4046708

Reply to the post : http://www.jboss.com/index.html?module=bb&op=posting&mode=reply&p=4046708



More information about the jboss-user mailing list