gpt4 book ai didi

java - 连接未使用configuration.setProperty ("hibernate.c3p0.preferredTestQuery"、 "SELECT 1")进行测试;

转载 作者:行者123 更新时间:2023-12-01 12:40:12 25 4
gpt4 key购买 nike

我以编程方式设置连接。

    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

从配置中可以看出,连接必须按照以下进行测试 configuration.setProperty("hibernate.c3p0.preferredTestQuery", "SELECT 1");然而它没有经过测试。为什么没有查询到SELECT 1?

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

最佳答案

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

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

关于java - 连接未使用configuration.setProperty ("hibernate.c3p0.preferredTestQuery"、 "SELECT 1")进行测试;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25184786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com