繁体   English   中英

Spring TransactionRequiredException异常

[英]Spring TransactionRequiredException exception

这是我的存储库配置:

@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吗?

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

编辑

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

  • 在配置文件上使用@EnableTransactionManagement
  • 声明一个EntityManagerFactoryBean
  • 声明使用您先前声明的EntityManagerFactoryBean创建的JpaTransactionManager (该bean必须命名为transactionManager
  • (使用@PersistenceContext@Autowired )在您的@Repository (必须是spring bean)中注入一个EntityManager
  • @Service调用存储库,并使用带有@Transactional注释的公共方法

当然,这得到了简化,并且我假设您正在使用Java配置,注释和自动组件扫描。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM