Transaction in spring mvc and hiberante

I am using spring mvc 3.0 and sleep mode. I am here, like, I have an inventory where the user can add and remove items. For example, the total number = 50. Now two users at the same time want to update the inventory, for example, 2 items deleted by A and 4 items deleted by B. Thus, the total number = 44. Now, how can I complete this transaction when both users try simultaneously update inventory ?? If the transaction is not supported, it will be 50-2 = 48, and then 50-4 = 46.

+3
source share
2 answers

Use the version property to provide the correct semantics. This is the theme for the entire Hibernate reference section: "Optimistic concurrency control"

+2
source

You can use optimistic locking with version control. Optimistic version locking is enabled as soon as you add or property to map constant classes. The property mapping in XML should be placed immediately after the identifier property is displayed:

<class name="Item" table="ITEM">
    <id .../>
    <version name="version" access="field" column="OBJ_VERSION"/>
     ...
</class>

In this case, when the item is updated, SQL will:

update ITEM set INITIAL_PRICE='12.99', OBJ_VERSION=2
where ITEM_ID=123 and OBJ_VERSION=1

, OBJ_VERSION 1, . , JDBC , , - StaleObjectStateException.

, Hibernate , ( ). , ( , ). , optimistic-lock :

<class name="Item" table="ITEM" optimistic-lock="all">
<id .../>
...
</class>

SQL :   update ITEM set ITEM_PRICE = '12.99 '    ITEM_ID = 123    ITEM_PRICE = '9.99'    ITEM_DESCRIPTION = ""    ...    SELLER_ID = 45

, StaleObjectStateException. , , , .

"Java Persistent with Hibernate" CHRISTIAN BAUER GAVIN KING. ()

EDIT: . .

-1

All Articles