Java 未使用configuration.setProperty(“hibernate.c3p0.preferredTestQuery”和“SELECT 1”)测试连接;

Java 未使用configuration.setProperty(“hibernate.c3p0.preferredTestQuery”和“SELECT 1”)测试连接;,java,hibernate,c3p0,Java,Hibernate,C3p0,我以编程方式设置连接 configuration.setProperty("hibernate.connection.url", "jdbc:mysql://" + host + ":" + port + "/" + dbName); LOG.debug("{} Connection URL is jdbc:mysql://{}:{}/{}", Options.TAG, host, port, dbName); configuration.setProperty("hib

我以编程方式设置连接

    configuration.setProperty("hibernate.connection.url", "jdbc:mysql://" + host + ":" + port + "/" + dbName);
    LOG.debug("{} Connection URL is jdbc:mysql://{}:{}/{}", Options.TAG, host, port, dbName);
    configuration.setProperty("hibernate.connection.username", user);
    configuration.setProperty("hibernate.connection.password", pass);
    configuration.setProperty("hibernate.c3p0.preferredTestQuery", "SELECT 1");
    configuration.setProperty("hibernate.c3p0.min_size", minConns);
    configuration.setProperty("hibernate.c3p0.max_size", maxConns);
    configuration.setProperty("hibernate.c3p0.max_statements", "50");
    configuration.setProperty("hibernate.connection.driver_class", "com.mysql.jdbc.Driver");
    configuration.setProperty("hibernate.connection.pool_size", "1");
    configuration.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
    configuration.setProperty("hibernate.current_session_context_class", "thread");
    configuration.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.internal.NoCacheProvider");
    configuration.setProperty("hibernate.show_sql", "false");
    configuration.addResource("TransactionBean.hbm.xml");
    ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
            configuration.getProperties()).build();

    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
我收到警告

WARN BasicResourcePool:1851 - com.mchange.v2.resourcepool.BasicResourcePool$ScatteredAcquireTask@6972975c
-- Acquisition Attempt Failed!!! Clearing pending acquires. 
While trying to acquire a needed new resource, we failed to succeed more than the
maximum number of allowed acquisition attempts (30). Last acquisition attempt 
exception: 
com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: 
Communications link failure
从配置中可以看出,必须按照 setProperty(“hibernate.c3p0.preferredTestQuery”,“选择1”); 但是它没有经过测试。为什么不查询SELECT 1


我的目标是在连接尚未建立时获取异常

您已经设置了一个
preferredTestQuery
,如果将连接设置为要测试,则将使用该查询,但您实际上没有指定任何连接测试。为此,需要设置
testConnectionOnCheckin
testConnectionOnCheckout
,和/或
idleConnectionTestPeriod
。请看

哦。如果连连接都没有获得,你永远不会看到连接测试失败。客户端将无法从池中签出连接。(我想,当我第一次回答时,我错过了让您感到不安的警告。)在运行任何连接测试之前,池中必须有连接。使用
testConnectionOnCheckout=true
,客户端看到的每个连接都将经过测试。但连接获取失败是一个完全不同的问题(上面的警告)


如果您的池无法获取任何连接,客户端将挂起或超时(如果您设置了
checkoutTimeout
)。如果池通常获取连接,但偶尔由于某些原因无法获取连接,而这些原因通常不会使已获取的连接无效,则测试将正常运行并通过,而获取新连接的尝试有时会失败。如果采集失败表示DBMS已关闭或不可用,则当客户端尝试使用现在已断开的连接时,您将看到此警告和连接测试失败。

I添加了testConnectionOnCheckin=false TestConnectionOnOnCheckout=true idleConnectionTestPeriod=0,我可以从日志中看到c3p0池已使用此参数初始化。但我还是没有得到例外。也许我需要运行一些方法来测试显式运行连接测试?哦。如果连连接都没有获得,你永远不会看到连接测试失败。客户端将无法从池中签出连接。(我想,当我第一次回答时,我错过了让您感到不安的警告。)在运行任何连接测试之前,池中必须有连接。使用
testConnectionOnCheckout=true
,客户端看到的每个连接都将经过测试。但是,连接获取失败(上面的警告)是一个完全不同的问题。如果池无法获取任何连接,客户端将挂起或超时(如果您设置了
checkoutTimeout
)。如果池通常获取连接,但偶尔由于某些原因无法获取连接,而这些原因通常不会使已获取的连接无效,则测试将正常运行并通过,而获取新连接的尝试有时会失败。如果采集失败表示DBMS已关闭或不可用,则当客户端尝试使用现在已断开的连接时,您将看到此警告和连接测试失败。谢谢,现在一切都清楚了。你能把你的评论移到答案上吗?我会把它标记为正确答案?