Spring TransactionRequiredException异常

时间:2015-09-22 16:27:01

标签: java spring exception transactions spring-data-jpa

这是我的存储库配置:

@Configuration
public class RepositoryConfing {
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter){
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        entityManagerFactoryBean.setPackagesToScan("com.imdb.model");
        return entityManagerFactoryBean;
    }

    @Bean
    public BasicDataSource dataSource(){
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("org.postgresql.Driver");
        ds.setUrl("jdbc:postgresql://localhost:5432/imdb");
        ds.setUsername("***");
        ds.setPassword("***");
        ds.setInitialSize(5);
        return ds;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter(){
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.POSTGRESQL);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(false);
        adapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");
        return adapter;
    }
}

当我调用merge方法时,我得到一个例外:javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call

可能是我的RepositoryConfig缺少一些其他配置?

编辑: 我的新存储库配置:

@Configuration
@EnableTransactionManagement
public class RepositoryConfing {
    @Bean
    public LocalContainerEntityManagerFactoryBean entityManagerFactoryBean(DataSource dataSource, JpaVendorAdapter jpaVendorAdapter){
        LocalContainerEntityManagerFactoryBean entityManagerFactoryBean = new LocalContainerEntityManagerFactoryBean();
        entityManagerFactoryBean.setDataSource(dataSource);
        entityManagerFactoryBean.setJpaVendorAdapter(jpaVendorAdapter);
        entityManagerFactoryBean.setPackagesToScan("com.imdb.model");
        return entityManagerFactoryBean;
    }

    @Bean
    public BasicDataSource dataSource(){
        BasicDataSource ds = new BasicDataSource();
        ds.setDriverClassName("org.postgresql.Driver");
        ds.setUrl("jdbc:postgresql://localhost:5432/imdb");
        ds.setUsername("***");
        ds.setPassword("***");
        ds.setInitialSize(5);
        return ds;
    }

    @Bean
    public JpaVendorAdapter jpaVendorAdapter(){
        HibernateJpaVendorAdapter adapter = new HibernateJpaVendorAdapter();
        adapter.setDatabase(Database.POSTGRESQL);
        adapter.setShowSql(true);
        adapter.setGenerateDdl(false);
        adapter.setDatabasePlatform("org.hibernate.dialect.PostgreSQLDialect");
        return adapter;
    }

    @Bean
    public PlatformTransactionManager txManager() {
        return new DataSourceTransactionManager(dataSource());
    }
}

现在它失败了StackOverflow。可能DataSourceTransactionManager不适合JPA吗?

1 个答案:

答案 0 :(得分:6)

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
}
  • 您的txManager方法应重命名为transactionManager
  • 您需要JpaTransactionManager而不是DataSourceTransactionManager

修改

为了与JPA进行全功能交易,您需要:

  • 在配置文件中使用@EnableTransactionManagement
  • 声明EntityManagerFactoryBean
  • 声明使用之前声明的JpaTransactionManager
  • 创建的transactionManager(bean必须命名为EntityManagerFactoryBean
  • @PersistenceContext(必须是春天豆)中注入(@AutowiredEntityManager@Repository
  • @Service调用您的存储库,并使用@Transactional
  • 分配的公开方法

当然这是简化的,我假设你使用的是java配置,注释和自动组件扫描。