1 Reply Latest reply on May 7, 2013 6:55 PM by michael_jboss

    select for update

    michael_jboss

      Hello.

      I use jboss as 7.1.1

      I Have test stateless ejb:

       

      @Stateless(name = BalanceBeanRemote.NAME, mappedName = BalanceBeanRemote.MAPPED_NAME)

      @Remote(BalanceBeanRemote.class)

      @TransactionAttribute(value = TransactionAttributeType.REQUIRES_NEW)

      @TransactionManagement(TransactionManagementType.CONTAINER)

      public class BalanceBean implements BalanceBeanRemote{

       

      public void testBalance(){

              TestBalance testBalance = em.find(TestBalance.class, 1L, LockModeType.PESSIMISTIC_WRITE);

       

              try {

                  TimeUnit.SECONDS.sleep(2);

              } catch (InterruptedException e) {

                  e.printStackTrace();

              }

       

              testBalance.setSmsAmount(testBalance.getAmount() - 9);

              testBalance.setLockSmsAmount(testBalance.getLockAmount() + 9);

          }

       

      }

       

      I called the method and i saw in console "select for update". It was right.

      If i was calling this method two times at the same time then the lock was not work.

      Please help me to understand this problem.

       

       

      After that i used a simple jdbc(with query "select for update") example.

      When transaction commited for the first thread and only after this in the second thread was doing the second "select for update". It is right:

       

      public class Test{

      public statis void test() throws Exception {

              Class.forName("com.mysql.jdbc.Driver");

              Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "user", "psw");

              connection.setAutoCommit(false);

              Statement stmt = connection.createStatement();

              ResultSet rs = stmt.executeQuery("select * " +

                      "from test_balance " +

                      "where id=1 for update ");

       

              while(rs.next()){

                  System.out.println(rs.getString(1));

              }

       

              TimeUnit.SECONDS.sleep(2);

              connection.commit();

      }

       

      public static void main(String[] args) throws Exception {

       

              ExecutorService executorService = Executors.newFixedThreadPool(10);

              for(int i = 0; i < 2; i++){

              executorService.execute(new Runnable() {

                      public void run() {

                          try {

                              new TestConnect().test();

                          } catch (Exception e) {

                              e.printStackTrace();

                          }

                      }

                  });

              }

              executorService.shutdown();

      }

      }

       

      Maybe did not i do some configuration for jboss?

      Please help me.