Spring No Transaction Autocommit

时间:2014-09-05 10:45:38

标签: java spring hibernate jpa jdbc

我正在使用带有JPA的Spring 3。 JPA供应商是Hibernate。

当尝试持久化数据时,我想将实体提交到数据库,并将autocommit设置为true。我不想使用这些交易。

我有以下代码示例:

@Configuration
public class EntityManagerFactoryConfig {

@Autowired
private HibernateConfig hibernateConfig;
@Autowired
private DataSourceConfig dataSourceConfig;

public EntityManagerFactoryConfig() {

}

private Properties getDefaultJpaProperties() {
    Properties jpaProperties = new Properties();
    jpaProperties.setProperty("hibernate.show_sql", hibernateConfig.getHibernateShowSQL());
    jpaProperties.setProperty("hibernate.dialect", hibernateConfig.getHibernateDialect());
    jpaProperties.setProperty("hibernate.hbm2ddl.auto", hibernateConfig.getHibernateHbm2DDL());
    jpaProperties.setProperty("hibernate.connection.autocommit", "true");
    jpaProperties.setProperty("javax.persistence.jdbc.driver", dataSourceConfig.getJdbcDriverClassName());
    jpaProperties.setProperty("javax.persistence.jdbc.user", dataSourceConfig.getJdbcUsername());
    jpaProperties.setProperty("javax.persistence.jdbc.password", dataSourceConfig.getJdbcPassword());
    return jpaProperties;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() throws Exception {
    LocalContainerEntityManagerFactoryBean localContainerEntityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    localContainerEntityManagerFactoryBean.setDataSource(dataSourceConfig.dataSource());
    localContainerEntityManagerFactoryBean.setJpaVendorAdapter(hibernateConfig.jpaVendorAdapter());
    Properties jpaProperties = getDefaultJpaProperties();
    jpaProperties.setProperty("javax.persistence.jdbc.url", dataSourceConfig.getJdbcUrlDefault());
    localContainerEntityManagerFactoryBean.setJpaProperties(jpaProperties);
    localContainerEntityManagerFactoryBean.setPersistenceUnitName("defaultpersistenceunit");
    return localContainerEntityManagerFactoryBean;
}

当将hibernate.connection.autocommit设置为true时,Spring会抛出以下异常:

javax.persistence.TransactionRequiredException: no transaction is in progress
at org.hibernate.ejb.AbstractEntityManagerImpl.flush(AbstractEntityManagerImpl.java:959)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:366)
at com.sun.proxy.$Proxy28.flush(Unknown Source)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)
at com.sun.proxy.$Proxy28.flush(Unknown Source)
at com.persistence.dao.hibernate.GenericHibernateDAO.save(GenericHibernateDAO.java:44)
at com.processor.RouterNoApacheCamelIT.setUp(RouterNoApacheCamelIT.java:51)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:27)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)

我不想使用@Transactional。我只是想在不使用事务的情况下提交数据。

我似乎无法在互联网上找到任何东西。

这怎么可能?

0 个答案:

没有答案