我在 Tomcat 6 上使用 Hibernate 3.3.2 和 Spring 3.2.3。我想使用连接池并决定测试 Oracle 的 UCP(适用于 Oracle 11.2.0.4)。
(我在这篇文章的末尾添加了编辑,因为经过一些发展后情况发生了变化)
我无法摆脱自动提交。我尝试在 Tomcat 的 context.xml 中像这样配置数据源:
<Resource
name="jdbc/datasource"
auth="Container"
type="oracle.ucp.jdbc.PoolDataSource"
[snip]
initialPoolSize="3"
minPoolSize="3"
maxPoolSize="10"
factory="oracle.ucp.jdbc.PoolDataSourceImpl"
connectionFactoryClassName="oracle.jdbc.pool.OracleDataSource"
connectionProperties=";AutoCommit=false;"
/>
注意分号。因为这篇文章,我添加了它们。但它不起作用,无论有分号还是没有分号。
在我的应用程序中,我有一些这样的测试代码:
PoolDataSourceImpl pds;
try {
pds = (PoolDataSourceImpl) hc.dataSource();
System.out.println("Print all conn props:" + pds.getConnectionProperties().toString());
} catch (NamingException e) {
e.printStackTrace();
}
sessionFactory.getCurrentSession().doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
System.err.println("################################### autocommit is " + connection.getAutoCommit());
}
});
输出是:
Print all conn props:{AutoCommit=false}
################################### autocommit is true
我还尝试使用 hibernate.connection.autocommit=false 在 Hibernate 中禁用自动提交,但这也不起作用。
编辑:我的配置如下:
@Configuration
public class HibernateConfig {
@Bean
LocalSessionFactoryBean hibernateSessionFactory() throws Exception {
LocalSessionFactoryBean factory = new LocalSessionFactoryBean();
factory.setMappingResources(new String[] { "mapping.hbm.xml" });
factory.setHibernateProperties(hibernateProperties());
factory.setDataSource(dataSource());
return factory;
}
@Bean
public DataSource dataSource() throws NamingException {
return (DataSource) new InitialContext().lookup("jdbc/datasource");
}
@Bean
public HibernateTransactionManager transactionManager() throws Exception {
HibernateTransactionManager manager = new HibernateTransactionManager();
manager.setSessionFactory(hibernateSessionFactory().getObject());
return manager;
}
@Bean
Properties hibernateProperties() throws IOException {
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource("hibernate.properties"));
bean.afterPropertiesSet();
return bean.getObject();
}
和
@Configuration
@EnableTransactionManagement
@EnableScheduling
@Import({ HibernateConfig.class, ... })
@ComponentScan(basePackages = ...)
public class ServerCommonsConfig {
[...]
}
和
@Configuration
@EnableAspectJAutoProxy(proxyTargetClass = true)
@Import({ ServerCommonsConfig.class, ... })
public class ApplicationServerConfig {
[...]
}
hibernate.properties 看起来像这样:
# Hibernate Properties
#hibernate.bytecode.provider=cglib
hibernate.dialect=org.hibernate.dialect.Oracle10gDialect
hibernate.show_sql=true
hibernate.format_sql=true
hibernate.cache.provider_class=org.hibernate.cache.NoCacheProvider
hibernate.cache.use_second_level_cache=false
hibernate.cache.use_query_cache=false
hibernate.jdbc.batch_size=200
hibernate.connection.autocommit=false
connection.autocommit=false
编辑2:
显然 autoCommit 属性的名称有错字。肯定是
connectionProperties=";autoCommit=false;"
开头有一个小“a”。我使用了大写版本,因为我发现这是在网上配置 UCP 且 autoCommit off 的唯一示例之一。
现在测试输出是
Print all conn props:{autoCommit=false}
false
################################### autocommit ist false
这一切都很好,花花公子,但现在没有任何承诺。我在刷新时看到 Hibernate 正在编写 DELETE 语句,在 @Transactional 方法结束后,我可以调试到 Spring 的 TransactionAspectSupport 和 HibernateTransactionManager.doCommit(),最后是 JDBCTransaction.commitAndResetAutoCommit() 它说
private void commitAndResetAutoCommit() throws SQLException {
try {
jdbcContext.connection().commit();
}
finally {
toggleAutoCommit();
}
}
,但数据库对此并不担心。不提交任何更改。
数据访问代码示例(这是客户端通过 Spring HTTP 调用程序调用的服务器上的服务):
@Transactional
@Component
public class ServiceImpl implements Service {
@Resource
private SessionFactory sessionFactory;
//added this method to test the autocommit issue
@Override
public List<Stuff> getStuff(Long id) {
Query query = sessionFactory.getCurrentSession().createQuery(
"FROM Stuff p");
@SuppressWarnings("unchecked")
List<Stuff> list = query.list();
for (Stuff stuff : list) {
sessionFactory.getCurrentSession().delete(stuff);
}
//this flush used to commit the changes
//instead, now nothing gets committed
sessionFactory.getCurrentSession().flush();
return null;
}
}