带有JPA的Spring 4(Hibernate实现):事务无法正常工作(没有正在进行的事务)

时间:2015-07-07 13:52:40

标签: spring jpa spring-transactions spring-annotations

好的,这看起来是一个重复的问题,然而,我一直在寻找这个超过2天但没有成功。以下是配置详细信息:

带注释的App Config类

@Configuration
@ComponentScan(basePackages = "com.test")
@EnableTransactionManagement(mode=AdviceMode.PROXY, proxyTargetClass=true)
public class AnnotatedAppConfig {

private static final Logger _logger = Logger
        .getLogger(AnnotatedAppConfig.class);

@Bean
public DataSource dataSource() {
    // C3P0 datasource configuration
    final ComboPooledDataSource dataSource = new ComboPooledDataSource();
    try {
        dataSource.setDriverClass(ReaderUtil.getInstance()
                .getProperty(IConst.DB_DRIVER));
    } catch (PropertyVetoException e) {
        _logger.error("Error setting driver class ", e);
    }
    dataSource.setUser(ReaderUtil.getInstance().getProperty(
            IConst.DB_USER));
    dataSource.setPassword(ReaderUtil.getInstance().getProperty(
            IConst.DB_PASSWORD));
    dataSource.setJdbcUrl(ReaderUtil.getInstance().getProperty(
            IConst.DB_URL));

    _logger.info("Datasource created successfully");
    return dataSource;
}

@Bean
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
    final LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
    entityManagerFactoryBean.setDataSource(dataSource());
    entityManagerFactoryBean.setPersistenceUnitName("testunit");
    entityManagerFactoryBean.setJpaVendorAdapter(createJPAVendorAdapter());

    _logger.info("EntityManagerFactory created successfully");
    return entityManagerFactoryBean;
}

@Bean
public PlatformTransactionManager txManager() {
    final JpaTransactionManager transactionManager = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(entityManagerFactory()
            .getObject());
    transactionManager.setDataSource(dataSource());

    _logger.info("Transaction Manager created successfully");
    return transactionManager;
}

private HibernateJpaVendorAdapter createJPAVendorAdapter() {
    final HibernateJpaVendorAdapter jpaVendorAdapter = new HibernateJpaVendorAdapter();
    jpaVendorAdapter.setShowSql(true);
    jpaVendorAdapter.setGenerateDdl(false);
    jpaVendorAdapter.setDatabase(Database.MYSQL);
    jpaVendorAdapter.setDatabasePlatform(ReaderUtil.getInstance()
            .getProperty(IConst.HIBERNATE_DB_DIALECT));
    return jpaVendorAdapter;
    }
}

带注释的服务类

@Service
@Transactional(value="txManager")
public class TestServiceImpl extends BaseServiceImpl implements ITestService {

@Autowired
private ITestDAO testDAO;

@Override
@Transactional(propagation=Propagation.REQUIRES_NEW, readOnly=false, value="txManager")
public Long register(final String username, final String password,
        final String name, final String address, final Integer deptId) {
    return testDAO.register(username, password, name, address, deptId);
    }
}

当我尝试调用register方法时,抛出以下错误(在DEBUG模式下):

  

TransactionAspectSupport.completeTransactionAfterThrowing(534)|在异常之后完成[com.test.service.TestServiceImpl.register]的事务:javax.persistence.TransactionRequiredException: 没有正在进行的事务   2015年7月7日18:59:36,488 230371 [http-bio-9080-exec-5]:DEBUG - RuleBasedTransactionAttribute.rollbackOn(131)|应用规则来确定事务是否应该在javax.persistence.TransactionRequiredException上回滚:没有事务正在进行中

我已经尝试了所有我能在网上找到的东西,但是,没有运气。 任何专家,请帮助我了解配置中缺少的内容。

请注意:自动装配工作正常,读取事务(SELECT查询)工作正常。

我读到@Service&如果在同一个类上一起应用,则@Transactional注释不起作用(在我的情况下发生,TestServiceImpl同时具有注释)。是这样的吗?在这种情况下,解决方法是什么?

0 个答案:

没有答案