gpt4 book ai didi

java - Hibernate 为每个延迟的查询执行连接

转载 作者:行者123 更新时间:2023-12-02 09:31:24 26 4
gpt4 key购买 nike

我正在将 swing 与 hibernate 结合使用。
因此,当我输入按钮时,它会连接,但再次按下按钮时,它会重复连接,并且需要几秒钟才能执行连接。
我不使用@annotations

    SessionFactory sessionFactory = new
Configuration().configure().buildseseienFactery ();

public Registration Login(String username, String password) {
Session session = sessionFactory.openSession ();
Criteria criteria = session.createCriteria(Registration.cLass);
criteria.add(Restrictions.eq ("username", username));
criteria.add (Restrictions.eg ("password", password));

Registration registration (Registration) criteria.uniqueResult();
session.close();
return registration;
}

我认为上面的 block 仅执行一次连接,但是当Login()时方法调用它确实再次执行。
那么如何才能只执行一次连接呢?

日志如下:

Sep 15, 2019 12:07:21 PM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.4.Final}
Sep 15, 2019 12:07:21 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.3.1.Final}
Sep 15, 2019 12:07:21 PM org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
Sep 15, 2019 12:07:21 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Sep 15, 2019 12:07:21 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Sep 15, 2019 12:07:21 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Registration.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Sales.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Items.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Customers.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Receivings.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Salestransaction.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Suppliers.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/cpos/pojo/Rctransaction.hbm.xml
Sep 15, 2019 12:07:22 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Sep 15, 2019 12:07:22 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH010002: C3P0 using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/CPOS?zeroDateTimeBehavior=convertToNull
Sep 15, 2019 12:07:22 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH000046: Connection properties: {user=root}
Sep 15, 2019 12:07:22 PM org.hibernate.c3p0.internal.C3P0ConnectionProvider configure
INFO: HHH000006: Autocommit mode: false
Sep 15, 2019 12:07:22 PM com.mchange.v2.log.MLog <clinit>
INFO: MLog clients using java 1.4+ standard logging.
Sep 15, 2019 12:07:22 PM com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.2.1 [built 20-March-2013 10:47:27 +0000; debug? true; trace: 10]
Sep 15, 2019 12:07:22 PM com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@5aa87035 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@27bcd446 [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, debugUnreturnedConnectionStackTraces -> false, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> z8kfsxa5ebtiu21xhrse6|7a3dc7, idleConnectionTestPeriod -> 0, initialPoolSize -> 5, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 1800, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 20, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 5, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@cfc29f4 [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> z8kfsxa5ebtiu21xhrse6|1a8ed8f, jdbcUrl -> jdbc:mysql://localhost:3306/CPOS?zeroDateTimeBehavior=convertToNull, properties -> {user=******} ], preferredTestQuery -> null, propertyCycle -> 0, statementCacheNumDeferredCloseThreads -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> z8kfsxa5ebtiu21xhrse6|12fe1cf, numHelperThreads -> 3 ]
Sep 15, 2019 12:07:23 PM org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect
Sep 15, 2019 12:07:23 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
Sep 15, 2019 12:07:23 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Sep 15, 2019 12:07:23 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory

有什么理由推迟吗?如何摆脱它?

最佳答案

很可能是因为每当您单击按钮时,您都会创建一个新的 SessionFactory

整个应用程序应该有一个SessionFactory。当应用程序启动时创建它使用 singleton pattern或 DI 框架(例如 Spring)等。这个想法是确保只创建一个 SessionFactory 实例。

如果代码需要访问 SessionFactory 来完成其工作,则应始终访问此 SessionFactory 实例,而不是创建一个新实例。

关于java - Hibernate 为每个延迟的查询执行连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57935770/

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