Hibernate查询在会话打开和关闭时停止而不连接

时间:2014-05-21 04:16:51

标签: oracle hibernate session grails transactions

我们有一个Grails应用程序,它使用Hibernate查询Oracle数据库。

一个相对简单的条件查询,在最糟糕的情况下,通过SQL Developer直接对数据库运行时需要二十分之一秒,从一小时到 30秒完成。

logSql设置为true并确认生成的SQL是高性能的(解释计划成本约为100,执行时间为10-30毫秒),我们仔细研究了Hibernate是什么最多可将其日志记录级别设置为TRACE

以下是我们在stdout中看到的内容:

******************** START HIBERNATE QUERY TRACE *****************
start time (millis): 1400772671767
2014-05-22 11:31:11,767 [http-bio-8080-exec-5] DEBUG transaction.JDBCTransaction  - begin
2014-05-22 11:31:11,767 [http-bio-8080-exec-5] DEBUG transaction.JDBCTransaction  - current autocommit status: true
2014-05-22 11:31:11,767 [http-bio-8080-exec-5] DEBUG transaction.JDBCTransaction  - disabling autocommit
2014-05-22 11:31:11,767 [http-bio-8080-exec-5] TRACE jdbc.JDBCContext  - after transaction begin
2014-05-22 11:31:11,770 [http-bio-8080-exec-5] TRACE loader.Loader  - querySpaces is [REDACTED]
2014-05-22 11:31:11,770 [http-bio-8080-exec-5] TRACE loader.Loader  - transformer=org.hibernate.transform.RootEntityResultTransformer returnTypes=[ REDACTED ] returnClasses=[ class REDACTED ]
2014-05-22 11:31:11,770 [http-bio-8080-exec-5] DEBUG jdbc.AbstractBatcher  - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
2014-05-22 11:31:11,771 [http-bio-8080-exec-5] DEBUG hibernate.SQL  - select REDACTED
2014-05-22 11:31:11,771 [http-bio-8080-exec-5] TRACE jdbc.AbstractBatcher  - preparing statement
2014-05-22 11:31:11,771 [http-bio-8080-exec-5] TRACE sql.BasicBinder  - binding parameter [1] as REDACTED
2014-05-22 11:31:11,771 [http-bio-8080-exec-5] TRACE sql.BasicBinder  - binding parameter [2] as REDACTED
2014-05-22 11:31:11,772 [http-bio-8080-exec-5] TRACE sql.BasicBinder  - binding parameter [3] as REDACTED
2014-05-22 11:31:11,772 [http-bio-8080-exec-5] TRACE loader.Loader  - Bound [4] parameters total
2014-05-22 11:31:12,323 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564865835008
2014-05-22 11:31:12,324 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:12,324 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:12,324 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564865839104
2014-05-22 11:31:12,324 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:12,324 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:14,331 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564874059776
2014-05-22 11:31:14,331 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:14,331 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:14,331 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564874059777
2014-05-22 11:31:14,331 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:14,332 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:16,339 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564882284544
2014-05-22 11:31:16,339 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:16,339 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:16,339 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564882284545
2014-05-22 11:31:16,340 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:16,340 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:18,348 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564890513408
2014-05-22 11:31:18,348 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:18,348 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:18,348 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564890513409
2014-05-22 11:31:18,349 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:18,349 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:20,356 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564898738176
2014-05-22 11:31:20,356 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:20,356 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:20,356 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564898738177
2014-05-22 11:31:20,357 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:20,357 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:22,364 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564906958848
2014-05-22 11:31:22,364 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:22,364 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:22,364 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564906962944
2014-05-22 11:31:22,364 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:22,364 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:24,373 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564915191808
2014-05-22 11:31:24,373 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:24,373 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:24,373 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564915191809
2014-05-22 11:31:24,374 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:24,374 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:26,379 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564923408384
2014-05-22 11:31:26,379 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:26,379 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:26,379 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564923408385
2014-05-22 11:31:26,379 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:26,379 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:28,387 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564931633152
2014-05-22 11:31:28,387 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:28,387 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:28,387 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564931633153
2014-05-22 11:31:28,387 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:28,388 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:30,395 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564939857920
2014-05-22 11:31:30,396 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:30,396 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:30,396 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564939862016
2014-05-22 11:31:30,396 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:30,396 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:32,403 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564948082688
2014-05-22 11:31:32,404 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:32,404 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:32,404 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564948086784
2014-05-22 11:31:32,404 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:32,404 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:34,408 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564956295168
2014-05-22 11:31:34,409 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:34,409 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:34,409 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564956299264
2014-05-22 11:31:34,409 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:34,409 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:36,416 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564964519936
2014-05-22 11:31:36,416 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:36,416 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:36,416 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564964519937
2014-05-22 11:31:36,416 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:36,417 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:38,421 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564972732416
2014-05-22 11:31:38,422 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:38,422 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:38,422 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564972736512
2014-05-22 11:31:38,422 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:38,422 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:40,428 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564980953088
2014-05-22 11:31:40,429 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:40,429 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:40,429 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564980957184
2014-05-22 11:31:40,429 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:40,429 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:42,436 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564989177856
2014-05-22 11:31:42,437 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:42,437 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:42,437 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564989181952
2014-05-22 11:31:42,437 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:42,437 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:44,441 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564997390336
2014-05-22 11:31:44,442 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:44,442 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:44,442 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737564997394432
2014-05-22 11:31:44,442 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:44,442 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:46,449 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737565005615104
2014-05-22 11:31:46,450 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:46,450 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:46,450 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737565005619200
2014-05-22 11:31:46,450 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:46,450 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:48,458 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737565013839872
2014-05-22 11:31:48,458 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:48,458 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:48,458 [http-bio-8080-exec-10] DEBUG impl.SessionImpl  - opened session at timestamp: 5737565013843968
2014-05-22 11:31:48,458 [http-bio-8080-exec-10] TRACE impl.SessionImpl  - closing session
2014-05-22 11:31:48,458 [http-bio-8080-exec-10] TRACE jdbc.ConnectionManager  - connection already null in cleanup : no action
2014-05-22 11:31:49,985 [http-bio-8080-exec-5] DEBUG jdbc.AbstractBatcher  - about to open ResultSet (open ResultSets: 0, globally: 0)
2014-05-22 11:31:49,986 [http-bio-8080-exec-5] TRACE loader.Loader  - processing result set
2014-05-22 11:31:49,986 [http-bio-8080-exec-5] TRACE loader.Loader  - done processing result set (0 rows)
2014-05-22 11:31:49,986 [http-bio-8080-exec-5] DEBUG jdbc.AbstractBatcher  - about to close ResultSet (open ResultSets: 1, globally: 1)
2014-05-22 11:31:49,986 [http-bio-8080-exec-5] DEBUG jdbc.AbstractBatcher  - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
2014-05-22 11:31:49,986 [http-bio-8080-exec-5] TRACE jdbc.AbstractBatcher  - closing statement
2014-05-22 11:31:49,986 [http-bio-8080-exec-5] TRACE loader.Loader  - total objects hydrated: 0
2014-05-22 11:31:49,986 [http-bio-8080-exec-5] DEBUG engine.StatefulPersistenceContext  - initializing non-lazy collections
2014-05-22 11:31:49,986 [http-bio-8080-exec-5] TRACE loader.Loader  - transformer=org.hibernate.transform.RootEntityResultTransformer returnTypes=[ REDACTED ] returnClasses=[ class REDACTED ]
2014-05-22 11:31:49,987 [http-bio-8080-exec-5] DEBUG transaction.JDBCTransaction  - commit
2014-05-22 11:31:49,987 [http-bio-8080-exec-5] TRACE impl.SessionImpl  - automatically flushing session
2014-05-22 11:31:49,987 [http-bio-8080-exec-5] TRACE def.AbstractFlushingEventListener  - flushing session
2014-05-22 11:31:49,987 [http-bio-8080-exec-5] REDACTED
2014-05-22 11:31:49,989 [http-bio-8080-exec-5] TRACE jdbc.ConnectionManager  - registering flush begin
2014-05-22 11:31:49,989 [http-bio-8080-exec-5] TRACE jdbc.ConnectionManager  - registering flush end
2014-05-22 11:31:49,989 [http-bio-8080-exec-5] TRACE def.AbstractFlushingEventListener  - post flush
2014-05-22 11:31:49,989 [http-bio-8080-exec-5] TRACE jdbc.JDBCContext  - before transaction completion
2014-05-22 11:31:49,989 [http-bio-8080-exec-5] TRACE impl.SessionImpl  - before transaction completion
2014-05-22 11:31:49,991 [http-bio-8080-exec-5] DEBUG transaction.JDBCTransaction  - re-enabling autocommit
2014-05-22 11:31:49,991 [http-bio-8080-exec-5] DEBUG transaction.JDBCTransaction  - committed JDBC Connection
2014-05-22 11:31:49,991 [http-bio-8080-exec-5] TRACE jdbc.JDBCContext  - after transaction completion
2014-05-22 11:31:49,991 [http-bio-8080-exec-5] DEBUG jdbc.ConnectionManager  - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
2014-05-22 11:31:49,992 [http-bio-8080-exec-5] TRACE impl.SessionImpl  - after transaction completion
2014-05-22 11:31:49,992 [http-bio-8080-exec-5] DEBUG impl.SessionImpl  - disconnecting session
2014-05-22 11:31:49,992 [http-bio-8080-exec-5] TRACE jdbc.ConnectionManager  - performing cleanup
2014-05-22 11:31:49,992 [http-bio-8080-exec-5] DEBUG jdbc.ConnectionManager  - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)]
2014-05-22 11:31:49,992 [http-bio-8080-exec-5] TRACE jdbc.JDBCContext  - after transaction completion
2014-05-22 11:31:49,992 [http-bio-8080-exec-5] DEBUG jdbc.ConnectionManager  - transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources!
2014-05-22 11:31:49,992 [http-bio-8080-exec-5] TRACE impl.SessionImpl  - after transaction completion
end time (millis): 1400772709992
difference: 38.225 seconds
******************* END HIBERNATE QUERY TRACE ********************

让我提请你注意引起我们注意的内容 - 大约每2秒重复6行 - opened session然后closing session然后connection already null,在跨度中完成两次一毫秒。

每2秒左右,Hibernate正在打开并立即关闭2个会话?我能正确地解释这个吗?在什么情况下它会做这样的事情?

DataSource.groovy中的数据源配置是基本的:

dataSource_REDACTED {
        driverClassName = 'oracle.jdbc.OracleDriver'
        dialect = 'org.hibernate.dialect.Oracle10gDialect'
        url = 'jdbc:oracle:thin:@REDACTED:1521/REDACTED'
        username = 'REDACTED'
        password = 'REDACTED'
        pooled = true
        properties {
            maxActive = 20
            minIdle = 1
            initialSize = 10
            testOnBorrow = true
            testWhileIdle = true
            validationQuery = "SELECT 1 FROM DUAL"
        }
}

没有properties {}阻止,因此无论使用空闲超时等属性的默认值如何。

同一个文件中还有一个hibernate配置:

hibernate {
    cache.use_second_level_cache = true
    cache.use_query_cache = true
    cache.provider_class = 'net.sf.ehcache.hibernate.EhCacheProvider'
} 

正如我之前所说,正在执行的条件查询非常简单:

withTransaction {
    List<MyAwesomeClass> awesomeThings = withCriteria {
        between("importantTime", startDate, endDate)
        eq("importantField", targetValue)
    }
 }

条件查询是从Grails域类中的方法运行的,该方法由控制器而不是事务服务直接调用,因此我将其包装在withTransaction中。

无论如何,简而言之,这里的交易是什么?为什么这会搞乱会话直到某事(不确定是什么)发生并且查询实际执行了?在尝试创建连接时,它是否与数据库本身的可用性有关?

1 个答案:

答案 0 :(得分:0)

这条线引起了我的注意:

transaction completed on session with on_close connection release mode; be sure to close the session to release JDBC resources! 
TRACE jdbc.JDBCContext  - after autocommit

根据Hibernate docs

  

ON_CLOSE - 基本上是上面描述的遗留行为。该   Hibernate会话在首次需要执行时获取连接   一些JDBC访问并保持该连接直到会话为止   闭合。

     

on_close - 说要使用ConnectionReleaseMode.ON_CLOSE。这个设置是   为了向后兼容而离开了,但是非常不鼓励使用它。

即使您没有在交易中运行,JPA也允许您执行查询,但我不建议您使用此设置。

我总是将所有Hibernate代码包装在Transaction中,对于只读操作,我选择readOnly = true标志。

每个线程似乎都会连续打开和关闭两个会话,如果这是因为你运行了两个连续的查询,你可以通过在同一个会话中运行它们来优化它。

因为您正在快速打开和关闭会话,所以您也打开和关闭JDBC连接,并且您的设置不包含任何连接池。除非您使用连接池,否则创建和销毁连接将非常昂贵。

查看我的benchmark on how connection pooling可以大大提高您的应用程序性能。

所以:

  1. 始终使用交易
  2. 使用连接池