how to use util concurrent package to replicate the load testing concurrent problems

Posted on 2013-08-27
Medium Priority
Last Modified: 2013-09-12
we have provided some RESTFUL webservices ,where client is going to consume these services.for this we have used spring restful services+JPA.
while doing load testing of 100 or more concurrent users we are facing exceptions like "LockAcquistionException" and
"Unable to commit: transaction marked for rollback". in normal scenarios its working fine but while doing load testing we are getting above exceptions

to replicate these scenarios we used Java Concurrent package and come up with the below code.
can we i use below code so that 100 or 200  users call these service concurrently?my intension is to replicate the scenario thats happening in load testing?

public class RestClientTest  
  ExecutorService executor = Executors.newFixedThreadPool(30);
  String	BASE_URL_DOC =null;
  RestTemplate restTemplate;
  public void updateDoc() throws Exception{
    try {
    	for(int i = 1; i <= 100; i++) {
            Runnable worker = new MyRunnable(i);
        // Wait until all threads are finish
        while (!executor.isTerminated()) {
        System.out.println("\nFinished all threads");
	} catch (Exception e) {

  public  class MyRunnable implements Runnable {
	  private int i;
      MyRunnable(int i) {
          this.i = i;

      public void run() {
    	  try {
    			   String	BASE_URL_DOC = "http://localhost:8080/docws/updateDocument/"+i+"/13131313";
        		   output= 	 restTemplate.getForObject(BASE_URL_DOC,Response.class);
          } catch (Exception e) {

Open in new window


//above restful websrevices internally calls this JPA code
public int updateDocument(Long empId, Long docId) {
		Query updateDocumentQuery = em.createNamedQuery("updateDocument");
		updateDocumentQuery.setParameter("empId", empId);
		updateDocumentQuery.setParameter("docId", docId);
		int updated = updateDocumentQuery.executeUpdate();
		if (updated != 0) {			
			recordHistory(empId, docId);			
		return updated;
	private void recordHistory(Long empId, Long docId) {
		Date currentDate = new Date();
		Query historyRecQuery = em.createNamedQuery("getHistoryRecord");
		historyRecQuery.setParameter("empId", empId);
		List results = (List) historyRecQuery.getResultList();
		if (!results.isEmpty()) {
			HistoryRecs historyRec = (HistoryRecs) results.get(0);
		Employee emp = em.find(Employee.class, empId); //some times here we are getting LockAcquistionException here
		HistoryRecs historyRecs = new HistoryRecs();
		if(emp.getpath() != null){

Open in new window

Question by:chaitu chaitu
LVL 36

Assisted Solution

mccarl earned 750 total points
ID: 39442236
can we i use below code so that 100 or 200  users call these service concurrently?
You can use that code, but you would need to change the number of threads allocated to the "executor". As it stands, it would only run (at most) 30 concurrent connections. The rest of the 'worker' object that you set it to execute, will wait in a queue for one of those 30 threads to become available.

As for the exceptions that you are getting, you really need to understand what all the JPA calls translate into on the DB side, and know what locking is performed by these calls so that you can analyse for possibility of deadlocks/livelocks/etc. And this will likely depend on what DB you are using too, as each has different locking semantics.
LVL 28

Accepted Solution

dpearson earned 750 total points
ID: 39443672
Like mccarl said, the issue will be the underlying SQL queries.

What's happening is that one thread is executing this code:

// This will involve locking some rows in the database during the update

while a second thread tries to execute this code:

Employee emp = em.find(Employee.class, empId);

One approach is to add "synchronized" to the two methods (updateDocument and recordHistory).  This will make them no longer execute at the same time, so you should no longer get the exception, but you'll also get worse throughput.


Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

607 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question