Spring Web应用程序中的死锁

时间:2012-08-09 13:21:03

标签: java spring spring-mvc thread-safety

如何编写线程安全的Spring Web应用程序?

当我们发送250个请求时,发生死锁。如何解决这个问题?

1.Controller - 该控制器接收来自呼叫者的请求。                当所有250个线程同时命中时。

public class TestController extends MultiActionController {

    private SyncService synchronizationService;
    public void setSynchronizationService(SyncService synchronizationService) {
        this.synchronizationService = synchronizationService;
    }
    public void StoreData(HttpServletRequest request, HttpServletResponse response) {
    byte[] syncData = null;
        byte[] returnValue = null;
        try {
    syncData = readData(request);
            returnValue = synchronizationService.receiveData(syncData);
        } catch (Exception e) {
        }
    }

2.Helper - 解析输入并填充表格。

        public class TestImpl implements SyncService {
    private DataParser dataParser = null;
    private QueueHelper queueHelper = null;

    public void setDataParser(DataParser dataParser) {
        this.dataParser = dataParser;
    }

   public void setQueueHelper(QueueHelper queueHelper) {
        this.queueHelper = queueHelper;
    }

 public byte[] receiveData(byte[] data) throws HibernateException, NumberFormatException {
        boolean addTransInQueue = false;
        String dataString = null;
        HashMap syncHashMap = null;
        try {

           InSyncModel syncModel = dataParser.parseData(dataString);
           addTransInQueue = queueHelper.addTransInQueue(syncModel, data);
           return "Succ".getBytes();
    } catch (JsonSyntaxException jse) {
              throw jse;
        }

    }
}

3.Configurations - 用于上述代码的bean注入。

   <bean name="synchronizationController" scope="request" class="TestController">
            <property name="synchronizationService" ref="synchronizationService"/>
   </bean>
     <bean name="synchronizationService" scope="request" class="TestImpl">
     <property name="dataParser" ref="dataParserObj"></property>
     <property name="queueHelper" ref="queueHelperObj"></property>
     </bean>


     <bean name="dataParserObj"  scope="request" class="DataParser"/>
     <bean name="queueHelperObj"  scope="request" class="QueueHelper">   
     <property name="dataSource" ref="dataSource"/>

     </bean>
     </beans>

1 个答案:

答案 0 :(得分:3)

  

如何解决这个问题?

通过识别死锁的位置。这可能不是Spring问题,而是代码中某处的死锁。

这是how to generate a thread dump。 JVM线程转储将突出显示哪些对象已死锁。