java Spring TransactionRequiredException 异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32722054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:40:29  来源:igfitidea点击:

Spring TransactionRequiredException exception

javaspringexceptiontransactionsspring-data-jpa

提问by serhii

Here is my Repository configuration:

这是我的存储库配置:

@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;
    }
}

When I call mergemethod, I get an exception: javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call

当我调用merge方法时,出现异常:javax.persistence.TransactionRequiredException: No EntityManager with actual transaction available for current thread - cannot reliably process 'merge' call

May be my RepositoryConfigis lacking of some additional configurations?

可能是我RepositoryConfig缺少一些额外的配置?

EDIT: My new Repository configuration:

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

@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());
    }
}

Now it fails with StackOverflow. May be DataSourceTransactionManageris not suit for JPA?

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

回答by Alexandre Jacob

@Bean
public PlatformTransactionManager transactionManager(EntityManagerFactory entityManagerFactory) {
    return new JpaTransactionManager(entityManagerFactory);
}
  • Your txManagermethod should be renamed to transactionManager.
  • You need to have a JpaTransactionManagerinstead of a DataSourceTransactionManager
  • 您的txManager方法应重命名为transactionManager.
  • 你需要有一个JpaTransactionManager而不是一个DataSourceTransactionManager

EDIT

编辑

In order to have fully functional transaction with JPA you need to :

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

  • Use @EnableTransactionManagementon your configuration file
  • Declare an EntityManagerFactoryBean
  • Declare a JpaTransactionManager(the bean must be named transactionManager) created with your previously declared EntityManagerFactoryBean
  • Inject (with @PersistenceContextor @Autowired) an EntityManagerin your @Repository(which must be a spring bean)
  • Call your repository from a @Serviceand use a publicmethod anotated with @Transactional
  • 使用@EnableTransactionManagement您的配置文件
  • 声明一个 EntityManagerFactoryBean
  • 声明一个JpaTransactionManager(bean 必须被命名transactionManager)用你之前声明的EntityManagerFactoryBean
  • 注射(有@PersistenceContext@Autowired)一个EntityManager在你的@Repository(必须是一个Spring bean)
  • 从 a 调用您的存储库@Service并使用带有注释的公共方法@Transactional

Of course this is simplified and I assume you are using java config, annotations, and autocomponent scanning.

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