This discussion is archived
5 Replies Latest reply: Mar 18, 2013 12:45 AM by Mr.Sam RSS

Pessimistic & Optimistic

Mr.Sam Newbie
Currently Being Moderated
Hi all.

Is this the place to post my question? Sorry i'm new here.
Just start to learn EJB and java.
I have some question about the Pessimistic & Optimistic and need some advice.
Here my sample code.

Basically, my UI will have 2 buttons and I open up 2 browser at a same time. Browser 1 will click on 1st button which is a SELECT statement and i want to lock the transaction.
Browser 2 will click on the 2nd button which is a SELECT statement that will query the same table with the button 1. The table consist of many data so the time to test is not a concern.

(Controller file)
Line: -----

//1st button
public String testCase1() {
List<EJBFILE> list;

try {
list = ejbFileFacade.ejbTestCase1();
} catch (Exception e) {
e.printStackTrace();
}

return "";
}

//2nd button
public String testCase2() {
try {
EJBFILE ejbFile = ejbFileFacade.ejbTestCase2();
EJBFILE.setViBranchCode("00");
ejbFileFacade.edit(itemVend);
} catch (Exception e) {
e.printStackTrace();
}

Line: -----

(EJB file)

public List<EJBFILE> ejbTestCase1() {
List<EJBFILE> list;
try {
TypedQuery<EJBFILE> userInfo = em.createNamedQuery("EJBFILE.findAll", EJBFILE.class);

userInfo.setLockMode(LockModeType.PESSIMISTIC_WRITE);
list = userInfo.getResultList();
return list;
} catch (Exception e) {
e.printStackTrace();
System.out.println("Inside ejbTestCase1 Exception");
return null;
}
}

public EJBFILE ejbTestCase2() {
String shortSku = "123";
String viStore = "123";
try {
TypedQuery<EJBFILE> userInfo = em.createNamedQuery("EJBFILE.findByExtra", EJBFILE.class);

userInfo.setParameter("shortSku", shortSku);
userInfo.setParameter("viStore", viStore);
return userInfo.getSingleResult();
} catch (Exception e) {
System.out.println("Inside ejbTestCase2 Exception");
e.printStackTrace();
return null;
}
}

Line: -----

What i expected is when the 2nd button is clicked, it will get an exception that the table has been lock?
But so far, the 2nd button can still process and didn't get any exception.

PESSIMISTIC_WRITE - Immediately obtain a long-term write lock on the data to prevent the data from being read, modified, or deleted.

Edited by: 994076 on Mar 15, 2013 3:11 PM
  • 1. Re: Pessimistic & Optimistic
    gimbal2 Guru
    Currently Being Moderated
    Why do you expect an exception? The second transaction will wait until the lock is released (which will be when the transaction started by the first button click ends) and then fetch the data.
  • 2. Re: Pessimistic & Optimistic
    Mr.Sam Newbie
    Currently Being Moderated
    I expect the second transaction will return an error message saying that the table has been lock?
    The problem is i use some SOP and i manage to capture the 1st transaction is in the process and the 2nd transaction is completed.
    Any advice?

    or is that anyway we can know the table is being locked?
  • 3. Re: Pessimistic & Optimistic
    gimbal2 Guru
    Currently Being Moderated
    994076 wrote:
    I expect the second transaction will return an error message saying that the table has been lock?
    I didn't ask you to repeat yourself, I asked WHY you think that should happen.
    The problem is i use some SOP and i manage to capture the 1st transaction is in the process and the 2nd transaction is completed.
    Any advice?
    Nope. Perform more research. Might be that your particular DBMS simply doesn't properly support this type of locking, or it will lock on the row and not the table.
  • 4. Re: Pessimistic & Optimistic
    Mr.Sam Newbie
    Currently Being Moderated
    "PESSIMISTIC_WRITE - Immediately obtain a long-term write lock on the data to prevent the data from being *read*, modified, or deleted."

    from above, I expect 2nd transaction will not be able to read it.
  • 5. Re: Pessimistic & Optimistic
    Mr.Sam Newbie
    Currently Being Moderated
    Hi gimbal2.

    "The second transaction will wait until the lock is released (which will be when the transaction started by the first button click ends) and then fetch the data."

    thanks for the comment and it help :D

    I test it out that when select a particular row, the row will be lock and 2nd transaction will wait until the lock is released.
    Is there any timeout configuration available to for an exception when the locking is longer than a period of time?

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points