2014-11-18 2 views
0

안녕하세요 Java 독립 실행 형 프로그램에 대한 연결 풀링을 만들었습니다.c3p0으로 연결 풀링을 처리하는 방법

그 particualr 데이터베이스 풀을 호출하는 모든 연결 .. 나는이 방법을 자주가는 두 differnt 한 데이터베이스 간의 getConnection(dbName)

Swithcing에게 전화를 할 때마다 내 DAO 클래스에서

private static ComboPooledDataSource = pooledDataSource; 

public static void createDataSource(String dbName) 
{ 

      if(dbName.equals("DB_ONE")) 
     { 
       pooledDataSource = new ComboPooledDataSource(); 

       // Here passing parameters will be different for DB_ONE    
      pooledDataSource.setDriverClass(dbDriver); 
      pooledDataSource.setJdbcUrl(dbUrl); 
      pooledDataSource.setUser(userID); 
      pooledDataSource.setPassword(password); 
      pooledDataSource.setMinPoolSize(Integer.parseInt(minCon)); 
      pooledDataSource.setMaxPoolSize(Integer.parseInt(maxCon)); 
      pooledDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime)); 
      pooledDataSource.setInitialPoolSize(Integer.parseInt(intialPoolSize)); 
      pooledDataSource.setPreferredTestQuery(testQuery); 
     } 
     if(dbName.equals("DB_TWO")) 
     { 
       pooledDataSource = new ComboPooledDataSource(); 

       // Here passing parameters will be different for DB_TWO    
      pooledDataSource.setDriverClass(dbDriver); 
      pooledDataSource.setJdbcUrl(dbUrl); 
      pooledDataSource.setUser(userID); 
      pooledDataSource.setPassword(password); 
      pooledDataSource.setMinPoolSize(Integer.parseInt(minCon)); 
      pooledDataSource.setMaxPoolSize(Integer.parseInt(maxCon)); 
      pooledDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime)); 
      pooledDataSource.setInitialPoolSize(Integer.parseInt(intialPoolSize)); 
      pooledDataSource.setPreferredTestQuery(testQuery); 

     } 

    } 

    public Connection getConnection(String dbName) 
    { 
      createDataSource(dbName); 
     return pooledDataSource.getConnection(); 

     } 

.. 더 명확하게 할 좋아지고있다 다시 만들었습니다.

I 중복 풀을 만들려면 그나마

...

어떻게 새로운 connection pool 생성 방지하기

?

도움이 고마워요. 방법 내부

+0

왜 'getConnection()'을 호출 할 때마다 새 풀이 생성된다고 했습니까? 'ComboPooledDataSource'객체가 정적이지 않습니까? –

+0

코드는 클래스로드시 한 번만 pooledDataSource를 만듭니다. 다른 클래스에도 정적 이니셜 라이저가 있습니까? – PeterMmm

+0

새 풀이 생성되었음을 어떻게 알 수 있습니까? – Antoniossss

답변

0

은 어쩌면 당신은 이런 일에 대해 생각할 수있는 필요하지만 (생성 후에는 계속 유지) 새 풀을 만들지 않고 원하는 데이터 소스에서 연결에 액세스 할 수 있습니다.

+1

다중 스레드 인 경우 적절한 동기화를 추가해야합니다. – pomkine

+0

@pomkine 그것은 좋은 충고입니다. 내 게시물을 편집했습니다. –

0

보기 : 당신은 당신이 새로운 연결을 생성하는 메서드를 호출 할 때마다

return pooledDataSource.getConnection(); 

. 그냥 pooledDataSource.getConnection(); , 당신은 경우에만 데이터 소스를 만들이 솔루션을

private static Map<String, ComboPooledDataSource> dataSources; 

static { 
    dataSources = new Map<String, ComboPooledDataSource>(); 
} 

public static void createDataSource(String dbName) { 
    ComboPooledDataSource pooledDataSource = new ComboPooledDataSource(); 
    if(dbName.equals("DB_ONE")) { 
     // Here passing parameters will be different for DB_ONE    
     pooledDataSource.setDriverClass(dbDriver); 
     pooledDataSource.setJdbcUrl(dbUrl); 
     pooledDataSource.setUser(userID); 
     pooledDataSource.setPassword(password); 
     pooledDataSource.setMinPoolSize(Integer.parseInt(minCon)); 
     pooledDataSource.setMaxPoolSize(Integer.parseInt(maxCon)); 
     pooledDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime)); 
     pooledDataSource.setInitialPoolSize(Integer.parseInt(intialPoolSize)); 
     pooledDataSource.setPreferredTestQuery(testQuery); 
     dataSources.put(dbName, pooledDataSource); 
    } else if(dbName.equals("DB_TWO")) { 
     // Here passing parameters will be different for DB_TWO    
     pooledDataSource.setDriverClass(dbDriver); 
     pooledDataSource.setJdbcUrl(dbUrl); 
     pooledDataSource.setUser(userID); 
     pooledDataSource.setPassword(password); 
     pooledDataSource.setMinPoolSize(Integer.parseInt(minCon)); 
     pooledDataSource.setMaxPoolSize(Integer.parseInt(maxCon)); 
     pooledDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime)); 
     pooledDataSource.setInitialPoolSize(Integer.parseInt(intialPoolSize)); 
     pooledDataSource.setPreferredTestQuery(testQuery); 
     dataSources.put(dbName, pooledDataSource); 
    } 
} 

public synchronized Connection getConnection(String dbName) { 
    if (!dataSources.containsKey(dbName)) { 
     createDataSource(dbName); 
    } 
    return dataSources.get(dbName).getConnection(); 
} 

: 정적 콘텐츠 내부 :

//change private variable to Connection 
private Connection connection 
static{ 

     pooledDataSource = new ComboPooledDataSource(); 

     pooledDataSource.setDriverClass(dbDriver); 
     pooledDataSource.setJdbcUrl(dbUrl); 
     pooledDataSource.setUser(userID); 
     pooledDataSource.setPassword(password); 
     pooledDataSource.setMinPoolSize(Integer.parseInt(minCon)); 
     pooledDataSource.setMaxPoolSize(Integer.parseInt(maxCon)); 
     pooledDataSource.setMaxIdleTime(Integer.parseInt(maxIdleTime)); 
     pooledDataSource.setInitialPoolSize(Integer.parseInt(intialPoolSize)); 
     pooledDataSource.setPreferredTestQuery(testQuery); 
     //here is new line 
     connection = pooledDataSource.getConnection(); 
} 

및 방법에

는 않습니다

public Connection getConnection() 
    { 
     return connection; 

     } 
관련 문제