Hibernate 4忽略了connection.autocommit=false的设置

5

我们有一个使用Java、Hibernate 4、Guice 3、guice-persist 3在Tomcat 6上的应用程序。

在hibernate.cfg.xml文件中,connection.autocommit被配置为false:

<session-factory>
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
    <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/dev</property>
    <property name="connection.username">root</property>
    <property name="connection.password">root</property>
    <property name="connection.autocommit">false</property>
    <property name="connection.proof_property">true</property>

    <!-- Disable second-level cache. -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>
    <property name="cache.use_query_cache">false</property>
    <property name="cache.use_minimal_puts">false</property>
    <property name="max_fetch_depth">3</property>

    <!-- Print SQL to stdout. -->
    <property name="show_sql">true</property>
    <property name="format_sql">true</property>

    <property name="id.new_generator_mappings">true</property>

    <!-- Drop and then re-create schema on SessionFactory build, for testing. -->
    <property name="hbm2ddl.auto">create</property>

    <!-- Bind the getCurrentSession() method to the thread. -->
    <property name="current_session_context_class">thread</property>

    <!-- configuration pool via c3p0--> 
    <property name="c3p0.initialPoolSize">0</property>
    <property name="c3p0.acquire_increment">1</property>
    <property name="c3p0.max_size">32</property>
    <property name="c3p0.max_statements">100</property> 
    <property name="c3p0.min_size">0</property> 
    <property name="c3p0.timeout">600</property> <!-- seconds --> 

    <!-- Hibernate XML mapping files -->
    <!-- <mapping resource="org/MyClass.hbm.xml"/>-->

    <!-- Hibernate Annotations (and package-info.java)-->
    <mapping package="model"/>

</session-factory>

但是在部署时,日志文件显示autocommit被设置为true:

INFO: Starting Servlet Engine: Apache Tomcat/6.0.35
28 janv. 2013 16:03:01 org.hibernate.annotations.common.Version <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {4.0.1.Final}
28 janv. 2013 16:03:01 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.1.9.Final}
28 janv. 2013 16:03:01 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
28 janv. 2013 16:03:01 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
28 janv. 2013 16:03:01 org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
28 janv. 2013 16:03:01 org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
28 janv. 2013 16:03:01 org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
28 janv. 2013 16:03:01 org.hibernate.service.jdbc.connections.internal.ConnectionProviderInitiator instantiateExplicitConnectionProvider
INFO: HHH000130: Instantiating explicit connection provider: org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider
28 janv. 2013 16:03:01 org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider configure
INFO: HHH010002: C3P0 using driver: com.mysql.jdbc.Driver at URL: jdbc:mysql://localhost:3306/dev
28 janv. 2013 16:03:01 org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider configure
INFO: HHH000046: Connection properties: {user=root, password=****, autocommit=true, proof_property=true, release_mode=auto}
28 janv. 2013 16:03:01 org.hibernate.service.jdbc.connections.internal.C3P0ConnectionProvider configure
INFO: HHH000006: Autocommit mode: true
28 janv. 2013 16:03:01 com.mchange.v2.log.MLog <clinit>
INFO: MLog clients using java 1.4+ standard logging.
28 janv. 2013 16:03:02 com.mchange.v2.c3p0.C3P0Registry banner
INFO: Initializing c3p0-0.9.1 [built 16-January-2007 14:46:42; debug? true; trace: 10]
28 janv. 2013 16:03:02 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
INFO: Initializing c3p0 pool... com.mchange.v2.c3p0.PoolBackedDataSource@b6868296 [ connectionPoolDataSource -> com.mchange.v2.c3p0.WrapperConnectionPoolDataSource@8d0e5680 [ acquireIncrement -> 1, 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 -> 1hgeby98szzd5hkbs91s5|a69b6b, idleConnectionTestPeriod -> 0, initialPoolSize -> 0, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 600, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 32, maxStatements -> 100, maxStatementsPerConnection -> 0, minPoolSize -> 0, nestedDataSource -> com.mchange.v2.c3p0.DriverManagerDataSource@d4b5ff9b [ description -> null, driverClass -> null, factoryClassLocation -> null, identityToken -> 1hgeby98szzd5hkbs91s5|75b3ee, jdbcUrl -> jdbc:mysql://localhost:3306/dev, properties -> {user=******, password=******, autocommit=true, proof_property=true, release_mode=auto} ], preferredTestQuery -> null, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false; userOverrides: {} ], dataSourceName -> null, factoryClassLocation -> null, identityToken -> 1hgeby98szzd5hkbs91s5|1a9bea3, numHelperThreads -> 3 ]
28 janv. 2013 16:03:02 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect
28 janv. 2013 16:03:02 org.hibernate.engine.jdbc.internal.LobCreatorBuilder useContextualLobCreation
INFO: HHH000424: Disabling contextual LOB creation as createClob() method threw error : java.lang.reflect.InvocationTargetException
28 janv. 2013 16:03:02 org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000268: Transaction strategy: org.hibernate.engine.transaction.internal.jdbc.JdbcTransactionFactory
28 janv. 2013 16:03:02 org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init>
INFO: HHH000397: Using ASTQueryTranslatorFactory
28 janv. 2013 16:03:02 org.hibernate.tool.hbm2ddl.SchemaExport execute
INFO: HHH000227: Running hbm2ddl schema export

为什么 Hibernate 忽略了自动提交属性?我们需要关心吗?(根据我们的测试,正确注释的方法中发生异常会导致回滚,所以似乎一切正常)

我认为在hibernate.cfg.xml中,属性名称应该从hibernate.autocommit改为hibernate.connection.autocommit,参考javadoc C3P0 - Madhusudana Reddy Sunnapu
当从hibernate.cfg.xml加载属性时,如果属性名不是以“hibernate.”开头,则会在属性名前加上“hibernate.”,以确保它们具有正确的配置键。此外,如果整个属性被忽略,则默认为false。 - Pace
我直接使用了你的cfg.xml文件,并创建了一个新项目,它按预期工作。当我使用单词“true”时,它记录为true,当我使用单词“false”时,它记录为false。所以问题不在你发布的任何内容中。在StandardServiceRegistryBuilder.configure(LoadedConfig loadedConfig)中设置断点,查看该设置在该点的值是什么。如果它不是你期望的值,请查看它从哪里获取配置(向上爬取堆栈跟踪)。 - Pace
1个回答

0

我在使用Hibernate + MySQL时也遇到了这个问题。我将其设置为false,但结果却是true;我将其设置为true,但结果却是false。我看到了你的问题,但没有答案。我想分享一下我的解决方法:

1. 重启数据库服务器。

2. 清理项目。

3. 重启Web服务器。

我尝试了很多方法,实际上,我不知道为什么最终这个方法起作用了……只是分享一下。


可能不是很有帮助,但我最终使用了eclipselink和postgreSQL。无论如何,PostgreSQL速度更快。 - Benjamin Marwell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接