c3p0 풀링을 사용하여 최대 절전 모드를 통해 Postgres DB에 연결하는 응용 프로그램을 작성하고 있습니다. 메인 인터페이스가 나타나기 전에 유효한 DB 연결 설정과 DB 연결을 탐지하고 싶습니다. 설정이 유효하지 않은 경우 사용자에게 메시지를 표시하고 설정을 변경하거나 애플리케이션을 닫으라고 제안하고 싶습니다. 그러나 문제는 EntityManagerFactory
이 예외를 throw하지 않거나 실패한 연결 후에도 반환하지 않는다는 것입니다.JPA, c3p0 및 Postgres로 최대 절전 모드. 데이터베이스 연결 문제 감지
public void connect(ConnectionSettingsModel conSet) throws Exception {
Map<String, String> connectionProperties = new HashMap<>();
connectionProperties.put("javax.persistence.jdbc.url", conSet.getUrl());
connectionProperties.put("javax.persistence.jdbc.user", conSet.getUser());
connectionProperties.put("javax.persistence.jdbc.password", conSet.getPassword());
connectionProperties.put("hibernate.default_schema", conSet.getSchema());
System.out.println("Before creating EM");
EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("postgres-connect", connectionProperties);
EntityManager entityManager = entityManagerFactory.createEntityManager();
System.out.println("After creating EM");
}
C3P0 구성 persistence.xml
에 : 여기
<property name="hibernate.connection.provider_class" value="org.hibernate.connection.C3P0ConnectionProvider"/>
<property name="hibernate.c3p0.min_size" value="0"/>
<property name="hibernate.c3p0.max_size" value="10"/>
<property name="hibernate.c3p0.timeout" value="300"/>
<property name="hibernate.c3p0.idle_test_period" value="3000"/>
<property name="hibernate.c3p0.max_statements" value="50"/>
<property name="hibernate.c3p0.acquireRetryAttempts" value="1"/>
존재하지 않는 사용자 이름의 로그 예 :
Before creating EM
14:47:07,009 INFO [com.mchange.v2.log.MLog] - MLog clients using log4j logging.
14:47:07,239 INFO [com.mchange.v2.c3p0.C3P0Registry] - Initializing c3p0-0.9.5.2 [built 08-December-2015 22:06:04 -0800; debug? true; trace: 10]
14:47:07,303 INFO [com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource] - Initializing c3p0 pool... [email protected] [ connectionPoolDataSource -> [email protected] [ acquireIncrement -> 3, acquireRetryAttempts -> 1, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, contextClassLoaderSource -> caller, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, forceSynchronousCheckins -> false, identityToken -> 1hge3hi9nk1ku80noopkx|3185ce3, idleConnectionTestPeriod -> 3000, initialPoolSize -> 0, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 300, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 0, nestedDataSource -> [email protected] [ description -> null, driverClass -> null, factoryClassLocation -> null, forceUseNamedDriverClass -> false, identityToken -> 1hge3hi9nk1ku80noopkx|26e664, jdbcUrl -> jdbc:postgresql://localhost:5432/postgres, properties -> {user=******, password=******} ], preferredTestQuery -> null, privilegeSpawnedThreads -> false, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, extensions -> {}, factoryClassLocation -> null, identityToken -> 1hge3hi9nk1ku80noopkx|3d02a858, numHelperThreads -> 3 ]
квіт. 07, 2017 2:47:07 PM org.postgresql.Driver connect
SEVERE: Connection error:
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "user"
--code omitted
квіт. 07, 2017 2:47:07 PM org.postgresql.Driver connect
SEVERE: Connection error:
org.postgresql.util.PSQLException: FATAL: password authentication failed for user "user"
--code omitted
-- few attempts to connect
14:47:55,009 WARN [com.mchange.v2.resourcepool.BasicResourcePool] - Having failed to acquire a resource, [email protected] is interrupting all Threads waiting on a resource to check out. Will try again in response to new client requests.
이 메시지 뒤에는 예외 또는 r을 throw하지 않습니다. ,
나는 스레드와이 문제를 해결하기 위해 노력하지만 한 ...처럼 간단 수 있습니다 (재구성 할 필요가, 당신은 단지
EntityManagerFactory
번을 만들려면) 솔루션은 더욱 간단하고 단순 해 보입니다. 'DriverManager.getConnection (conSet.getUrl(), conSet.getPassword()). close()' – AndriiL줄을 세 번째로 해결했습니다. –