chaitu chaitu
asked on
how to use util concurrent package to replicate the load testing concurrent problems
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?
}
//above restful websrevices internally calls this JPA code
************************** ********** ********** ********** ****
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;
@Autowired
RestTemplate restTemplate;
@Test
public void updateDoc() throws Exception{
try {
for(int i = 1; i <= 100; i++) {
Runnable worker = new MyRunnable(i);
executor.execute(worker);
}
executor.shutdown();
// Wait until all threads are finish
while (!executor.isTerminated()) {
}
System.out.println("\nFinished all threads");
} catch (Exception e) {
e.printStackTrace();
}
}
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) {
e.printStackTrace();
}
}
}
}
//above restful websrevices internally calls this JPA code
**************************
@Transactional
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);
historyRec.setEndDate(currentDate);
em.persist(historyRec);
}
Employee emp = em.find(Employee.class, empId); //some times here we are getting LockAcquistionException here
HistoryRecs historyRecs = new HistoryRecs();
historyRecs.setEmpId(empId);
historyRecs.setDocId(docId);
if(emp.getpath() != null){
historyRecs.setpath(emp.getpath());
}
historyRecs.setFromDate(currentDate);
em.persist(historyRecs);
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.