3

멀티 스레드 환경에서 데이터베이스 연결 풀링을 처리하기 위해 c3p0을 사용하고 있습니다. 질문은 다른 풀링 라이브러리와 관련이 있지만, 이것이 내가 가지고있는 것입니다.연결 풀링 및 Thread.interrupt()

은 최근에 나는 C3P0 직접 또는 간접적으로 사용하는 등 스레드 interrupt 이온 처리를 구현하는 데 필요한 것, 그리고 interrupt()c3p0Datasource.getConnection() 풀에서 나에게 Connection를 가져 오기 위해 노력하고 잘 때 호출되는 경우, 그것이 InterruptedException를 throw 것으로 나타났습니다.

물론,이 때문에 wait()

at java.lang.Object.wait(Native Method) 
at com.mchange.v2.resourcepool.BasicResourcePool.awaitAvailable(BasicResourcePool.java:1414) 

쿨의 발생합니다. 문제는이 문제를 어떻게 적절히 처리 할 것인가입니다. 두 경우 모두 스레드가 종료되기 전에 트랜잭션을 계속하고 싶습니다. b) 중단하려고합니다.

나는 잘 작동하고있는 것처럼 보이는 해결책을 시도했다. (답변으로 게시 됨) 사실,이 주제가 닫혀 있다고 생각한다. 그렇지 않으면 감사합니다!

답변

3

나는 Connection 개의 요청을 1 초 이내에 실행하고, 풀 병목 상태인지 확인한 다음 interrupt()을 호출 할 때마다 SELECT를 실행하여 간단한 테스트를 수행했습니다. 내가 찾은 무엇

connection 객체가 스택 트레이스 나에게 awaitAvailable(..)에서 C3P0 붕괴를 보여줍니다에도 불구하고, 미세하고 InterruptedException이 잡힌 후 멋쟁이이었다. 바로이 순간에 나는 그들의 출처를 조사하고 있습니다. 물론 그들은 InterruptedException을 처리합니다. 그들은 심지어 적절한 경고를 던집니다.

WARNING: [email protected] -- an attempt to checkout a resource was interrupted, and the pool is still live: some other thread must have either interrupted the Thread attempting checkout! 

많은 단어들 사이에도 불구하고 여전히 살아 있다고 말하고 있습니다. 해결 됐어.

어쨌든 테스트입니다.

ComboPooledDataSource ds = new ComboPooledDataSource(); 

// testing with various pool sizes - same effect 
ds.setMinPoolSize(1); 
ds.setMaxPoolSize(5); 
ds.setInitialPoolSize(2); 

Thread connectingThread = new Thread() { 

    public void run() { 
     Connection cnxn = null; 
     while (true) { 
      try { 
       cnxn = ds.getConnection(); 
       System.out.println("Got connection.); 
       executeQuery(cnxn); 
      } catch (SQLException e) { 
       System.out.println("Got exception."); 
       e.printStackTrace(); 

       // SOLUTION: 
       Throwable cause = e.getCause(); 
       if (cause instanceof InterruptedException) { 
        System.out.println("Caught InterruptedException! Cnxn is " + cnxn); 

        // note that cnxn is a com.mchange.v2.c3p0.impl.NewProxyConnection 
        // also note that it's perfectly healthy. 
        // 
        // You may either want to: 
        // a) use the cnxn to submit your the query 

        executeQuery(cnxn); 
        cnxn.close() 

        // b) handle a proper shutdown 

        cnxn.close(); 

       } 
       break; 
      } 
     } 
    }; 
}; 

connectingThread.start(); 

try { 
    Thread.sleep(1000); 
} catch (InterruptedException e) {   e.printStackTrace();  } 

connectingThread.interrupt();