No bean named 'transactionManager' is defined

前端 未结 4 1862
囚心锁ツ
囚心锁ツ 2020-12-06 04:51

I have configured two persistent units with the entity managers set up as show below:



        
相关标签:
4条回答
  • 2020-12-06 04:56

    Actually, there is a way to use named TransactionManager with Spring Data JPA. This works for me:

    <bean id="myTransactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="myEntityManagerFactory" />
    </bean>
    <tx:annotation-driven transaction-manager="myTransactionManager"/>
    
    <jpa:repositories base-package="com.xxx.yyy" entity-manager-factory-ref="myEntityManagerFactory" transaction-manager-ref="myTransactionManager">
    </jpa:repositories>
    
    0 讨论(0)
  • 2020-12-06 05:07

    I use java configuration and specifying the transactionManagerRef was solution for me:

    @Configuration
    @EnableTransactionManagement
    @EnableJpaRepositories(
        entityManagerFactoryRef = "myCustomEntityManagerFactory",
        basePackages = {"ua.demitt.other.path.to.repos"},
        transactionManagerRef = "myCustomTransactionManager" )
    
    0 讨论(0)
  • 2020-12-06 05:19

    The default value for the transaction-manager attribute is transaction-manager. In your case, you should specify which transaction manager you want to use per method or service like this:

    @Service
    @Transactional(value="LiveTransactionManager") 
    class someClass...
    

    or

    @Transactional(value="ArchiveTransactionManager") 
    public void someMethod
    
    0 讨论(0)
  • 2020-12-06 05:19

    In your @Configuration file, if this is your dataSource:

    @Bean(name = "dataSource")
    public DataSource getDataSource() {
        return DataSourceBuilder
                .create()
                .username(username)
                .password(password)
                .url(url)
                .build();
    }
    

    this would be your transaction manager bean:

    @Bean(name = "DataSourceTransactionManager")
    public DataSourceTransactionManager getDataSourceTransactionManager() {
        return new DataSourceTransactionManager(getDataSource());
    }
    

    Then annotate the service method you would like to be transactional:

    @Transactional(transactionManager = "DataSourceTransactionManager", timeout = 60, rollbackFor = { Exception.class})
    
    0 讨论(0)
提交回复
热议问题