Question
How to prevent concurrent update in Hibernate

Answers

ersion checking used in hibernate when more then one thread trying to access same data.

For example :
User A edit the row of the TABLE for update ( In the User Interface changing data - This is user thinking time)
and in the same time User B edit the same record for update and click the update.
Then User A click the Update and update done. Chnage made by user B is gone.

In hibernate you can perevent slate object updatation using version checking.

Check the version of the row when you are upding the row.
Get the version of the row when you are fetching the row of the TABLE for update.
On the time of updation just fetch the version number and match with your version number ( on the time of fetching).

This way you can prevent slate object updatation.

Steps 1:
Declare a variable "versionId" in your Class with setter and getter.
public class Campign
{
private Long versionId;
private Long campignId;
private String name;
public Long getVersionId()
{
return versionId;
}
public void setVersionId(Long versionId)
{
this.versionId = versionId;
}

public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}

public Long getCampignId()
{
return campignId;
}
private void setCampignId(Long campignId)
{
this.campignId = campignId;
}
}

Step 2.
In the .hbm.xml file
< class name="beans.Campign" table="CAMPIGN" optimistic-lock="version" >
< id name="campignId" type="long" column="cid" >
< generator class="sequence" >
< param name="sequence" > CAMPIGN_ID_SEQ < /param >
< /generator >
< /id >
< version name="versionId" type="long" column="version" / >
< property name="name" column="c_name"/ >
< /class >

Step 3.
Create a coulmn name "version" in the CAMPIGN table.

Step 4.
In the code
// foo is an instance loaded by a previous Session
session = sf.openSession();
int oldVersion = foo.getVersion();
session.load( foo, foo.getKey() );
if ( oldVersion!=foo.getVersion ) throw new StaleObjectStateException();
foo.setProperty("bar");
session.flush();
session.connection().commit();
session.close();



You can handle StaleObjectStateException() and do what ever you want.
You can display error message.

Hibernate autumatically create/update the version number when you update/insert any row in the table.   Your Comment




More Software,Java Questions..
What is the inputsplit in map reduce software?

Can variables be used in Java without initialization?

What is the difference between java bean and enterprise java bean?

What is software configuration management?

How does the java database connectivity (jdbc) work?

What are the advantages of threads in java?

What Is Java Api For Xml-based Rpc (jax-rpc)?

How can you implement fine-grained auditing?

What is IBM’s simple explanation for Big Data’s four critical features?

What is static synchronized method in JDBC API? Give an example?

What does the NULLIF function do?

What happens if a start method is not invoked and the run method is directly invoked?

Should we override finalize method

what is the difference between mysql_fetch_array and mysql_fetch_object?

How will XML affect my document links?

What is object cloning in Java?

Why to use Style Sheets?

What are Filters in MVC?

Can you explain Application layer in OSI model?

How to define new testplan attributes?



Search
Can you Answer!!
  • Q Which company has won the coveted Indian Premier League (IPL) media rights for 2018-23?
  • Q Enumerate the role of Sodium minerals in our body
  • Q Mention any two affinities of pogonophora towards invertebrates and two towards
  • Q How To Define the Name and Server for a new DSN?
  • Q What needs to be done for having the fail-safe stance?
  • Q Define Scenario Testing.
  • Q What two-colour scarf did David Beckham wear in support of the anti-Glazer campaign when leaving the Old Trafford pitch after playing for Milan in their European Championship defeat by Manchester United?
  • Q The terms spooling and buffering come first under the processing of ______.
  • Q What is the regular expression sequence that loosely translates to "anything or nothing?"
  • Q Explain the term account payable?
  • Q What's the typical background of one of your sales reps?