Java Hibernate - 同时运行多个线程

时间:2017-06-12 16:42:16

标签: java hibernate

我试图通过从多个线程调用以下方法来测试我的SPRING MVC服务性能。该方法使用Hibernate获取记录,然后再次更新它。 但似乎所有线程都是按顺序执行,而不是并行执行。

我的服务方式

@Transactional
public String performOperation() {
     USER user = dao.findUsr("name");
     user.setMarks(50);
}

我的测试应用

*Round 1*
thread1.start() : For Only T1, It takes time to execute : 5 Sec

*Round 2*
thread1.start()
thread2.start() : With T1 and T2: 10 Sec

*Round 3*
thread1.start()
thread2.start()
thread3.start() : With T1, T2, T3: 15 sec

*Round 4*
thread1.start()
thread2.start()
thread3.start()
thread4.start() : With T1, T2, T3, T4: 20 sec

我的配置

jdbc.initial.pool.size=10
jdbc.min.pool.size=10
jdbc.max.pool.size=120

未为以下设置设置任何内容:因此为其设置默认值

- current_session_context_class
- cache

观察:即使每个线程有5000个循环,使用的最大数据库池大小为25.正如MySQL Dashboard中所观察到的那样

问题 如果你看到它没有并行执行。 Hibernate正在锁定我猜的行。你能提供任何指针来同时运行它。

1 个答案:

答案 0 :(得分:0)

To start the threads at exactly the same time i would recommend giving CyclicBarrier a try.

CyclicBarrier is "A synchronization aid that allows a set of threads to all wait for each other to reach a common barrier point".

Within that Doc is an example of how it works and runs. Make reference to that within your code. For example:

final CyclicBarrier gate = new CyclicBarrier(5);

Thread thread1 = new Thread(){
public void run(){
    gate.await(); // Waits until all parties have invoked await on this barrier.
    //Your Code    
}};
Thread thread2 = new Thread(){
public void run(){
    gate.await();
    //Your Code    
}};
Thread thread3 = new Thread(){
public void run(){
    gate.await();
    //Your Code    
}};
Thread thread4 = new Thread(){
public void run(){
    gate.await();
    //Your Code    
}};

thread1.start();
thread2.start();
thread3.start();
thread4.start();
gate.await();
相关问题