我在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;"
/>
请注意分号。因为this post我添加了它们。但它不起作用,既不用分号也不用。但
在我的应用程序中,我有一些像这样的测试代码:
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中使用hibernate.connection.autocommit = false禁用自动提交,但这也不起作用。
编辑:我的配置如下:
@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
EDIT2:
显然有一个拼写错误,其中包含autoCommit属性的名称。一定是
connectionProperties=";autoCommit=false;"
开头有一个小“a”。我使用了大写版本,因为我在one of the only examples of configuring UCP with autoCommit off on the net中找到了它。
现在测试输出
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;
}
}
答案 0 :(得分:0)
Spring会自动为您管理连接(假设您使用Spring控制的事务和spring配置中的数据源)。因此,您不需要乱用连接的只读设置。
此外,如果使用spring将数据源注入休眠状态,则hibernate.connection.*
属性无效。