No bean named 'transactionManager' available
Asked Answered
W

7

14

When I tried to create relationship using spring code, I am getting Transaction manager error. I am using Mysql and Neo4j database in my project. I tries different solution but not able to resolve.

org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'transactionManager' available: No matching PlatformTransactionManager bean found for qualifier 'transactionManager' - neither qualifier match nor bean name match!

Pom.xml file as below

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>1.5.10.RELEASE</version>
        </dependency>

        <!-- For Neo4J Graph Database -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-neo4j</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.9-rc</version>
        </dependency>

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
    <version>1.5.10.RELEASE</version>
</dependency>
 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
    </dependency>

    </dependencies>

    <properties>
        <java.version>1.8</java.version>
        <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
    </properties>
</project>

Application.class

    @SpringBootApplication
    @EnableTransactionManagement
    @EntityScan("projectone.entities")
    public class Application {

        public static void main(String[] args) {
            //For Starting application
            ApplicationContext applicationContext=SpringApplication.run(Application.class, args);

        }
    }

Database Configuration file:

@Configuration
@EnableJpaRepositories(basePackages = {"projectone.mysql"},
        entityManagerFactoryRef = "dbEntityManager",
        transactionManagerRef = "dbTransactionManager")
@EnableTransactionManagement
public class DatabaseConfiguration {

     @Autowired


 private Environment env;
        @Bean
        @Primary
        public LocalContainerEntityManagerFactoryBean dbEntityManager() {
            LocalContainerEntityManagerFactoryBean em = new LocalContainerEntityManagerFactoryBean();
            em.setDataSource(dbDatasource());
            em.setPackagesToScan(new String[]{"projectone.mysql"});
            em.setPersistenceUnitName("dbEntityManager");
            HibernateJpaVendorAdapter vendorAdapter = new HibernateJpaVendorAdapter();
            em.setJpaVendorAdapter(vendorAdapter);

            HashMap<String, Object> properties = new HashMap<>();

            properties.put("hibernate.dialect",env.getProperty("hibernate.dialect"));
            properties.put("hibernate.show-sql",env.getProperty("jdbc.show-sql"));


            em.setJpaPropertyMap(properties);
            return em;
        }
        @Primary
        @Bean
        public DataSource dbDatasource() {
            DriverManagerDataSource dataSource
                    = new DriverManagerDataSource();
            dataSource.setDriverClassName(
                    env.getProperty("spring.datasource.driverClassName"));
            dataSource.setUrl(env.getProperty("spring.datasource.url"));
            dataSource.setUsername(env.getProperty("spring.datasource.username"));
            dataSource.setPassword(env.getProperty("spring.datasource.password"));
            return dataSource;
        }
        @Primary
        @Bean
        public PlatformTransactionManager dbTransactionManager() {
            JpaTransactionManager transactionManager
                    = new JpaTransactionManager();
            transactionManager.setEntityManagerFactory(
                    dbEntityManager().getObject());
            return transactionManager;
        }

}

I tried minimal configuration by removing the database configuration class. After that my application is not running and I am getting Person is not a managed Bean error.

enter image description here

If I use only @SpringBootApplication annotation then I am getting following Exception: It is throwing

    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'mappingController': Unsatisfied dependency expressed through field 'branchService'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'branchServiceImplementaion': Unsatisfied dependency expressed through field 'branchRepository'; 

nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'branchRepository': Unsatisfied dependency expressed through method 'setSession' parameter 0; 

nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.data.neo4j.transaction.SharedSessionCreator#0': Cannot resolve reference to bean 'sessionFactory' while setting constructor argument; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.class]: Bean instantiation via factory method failed;

 nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.neo4j.ogm.session.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is NullPointerException
Wilmington answered 19/2, 2018 at 7:38 Comment(9)
Remove your DatabaseConfiguration class and place your @SpringBootApplication annotated class in the projectone base package.Chalet
PDO : #10720146Letsou
@M.Deinum My Application class is already in base package. If I remove database configuration class, Neo4J and MySQL entities conflicts and throws an error.Wilmington
You still don't need a full override of the spring configuration for that. I also don't see why they would conflict (could you add the exception).Chalet
@M.Deinum I will try again that configuration and will post the exceptionWilmington
@M.Deinum I did the configuration as you said and tried other similar things also but I am not able to run the project. Person is not a managed BEan. I also attached the screen shot of this error in this post.Wilmington
You have @EntityScan and that doesn't cover your mysql entities, so obviously you will get an exception. But why do you need this? The default configuration should be perfectly able to figure out what is Neo4J or JPA (the annotations and repositories are different).Chalet
@M.Deinum In the default case(Only SpringBootApplication), It is not able to find my Neo4jService. I am having "MappingController" where I have "@AutoWired branchService" object to Neo4j Service "BranchService" class. Detailed Error I posted in the question.Wilmington
@M.Deinum I have created separate question with basic configuration and getting some error. Please have a look #48920498Wilmington
W
25

Finally, I found the mistake:
Due to this method being named dbTransactionManager, Spring couldn't find it. I just had to change its name to transactionManager, by changing @Bean to @Bean(name = "transactionManager"), as shown below.

@Bean(name = "transactionManager")
public PlatformTransactionManager dbTransactionManager() {
    JpaTransactionManager transactionManager
            = new JpaTransactionManager();
    transactionManager.setEntityManagerFactory(
            dbEntityManager().getObject());
    return transactionManager;
}

Also: I could've simply renamed the entire method to transactionManager()

Wilmington answered 3/3, 2018 at 5:24 Comment(1)
Adding this solved my problem. But the real cause was that I had one transitive dependency including spring-cloud-gcp-data-datastore in my classpath which was causing many conflictsJerrylee
I
8

I had the same issue, but I was missing the transactionManagerRef = "dbTransactionManager" configuration in @EnableJpaRepositories

Isogonic answered 11/10, 2019 at 6:48 Comment(1)
This works when there is a custom datasource and transcation manager bean created which does not have the reference name as 'transactionManager'. For example : @ Configuration @ EnableTransactionManagement @ EnableJpaRepositories( transactionManagerRef = "customTxMgr", entityManagerFactoryRef = ".."}) public class InstrumentDatasourceConfig { @ Bean(name = "customTxMgr") public PlatformTransactionManager transactionManager(...){ } }Cockatoo
O
4

resolve: to change a name of function from:

public PlatformTransactionManager dbTransactionManager(){}

to:

public PlatformTransactionManager transactionManager(){}
Olden answered 10/6, 2020 at 11:41 Comment(0)
H
2

Checke transactionManager this name JPAConfig class.


    @Bean
    public PlatformTransactionManager transactionManager(EntityManagerFactory emf){
            return new JpaTransactionManager(emf);
    }

  
Highhanded answered 12/7, 2022 at 14:58 Comment(0)
C
1

This error message typically occurs when Spring is unable to find a TransactionManager bean with the name transactionManager.

When you use Spring's transaction management, Spring needs to know which transaction manager to use. You can specify this using the @Transactional annotation, but Spring also needs to know which bean to use as the transaction manager. By default, Spring looks for a bean named transactionManager.

There are a few possible reasons why Spring is unable to find a transactionManager bean:

No transaction manager bean defined: You may have forgotten to define a transaction manager bean in your Spring configuration. You can define a transaction manager bean by adding the @Bean annotation to a PlatformTransactionManager-returning method in one of your configuration classes.

Bean is not named transactionManager: You may have defined a transaction manager bean, but you have given it a different name. In this case, you can either rename the bean to transactionManager or you can specify the name of the bean in your @Transactional annotation using the value or qualifier attribute.

Multiple transaction manager beans defined: You may have defined multiple transaction manager beans in your Spring configuration, and Spring is unable to determine which one to use. In this case, you can specify the name of the bean you want to use in your @Transactional annotation using the value or qualifier attribute.

To fix the error, you need to ensure that a transaction manager bean is defined in your Spring configuration and that it is named transactionManager. If the bean has a different name, you need to specify the name of the bean in your @Transactional annotation. If there are multiple transaction manager beans defined, you need to specify the name of the bean you want to use.

Coolish answered 3/5, 2023 at 12:56 Comment(1)
I wonder why this answer does not have upvotesFaker
U
0

I had the same problem and I noticed that there is an extra space where while specifying the transaction-manager as specified in the image. enter image description here

Uraninite answered 11/9, 2018 at 10:11 Comment(0)
I
0

In my case, the issue was some kind of conflict between Spring Data Firestore and spring data JPA. When I started the project I used the Google Firebase data base. Then I switched to MySQL however I didn't think that the dependency could conflict so I removed

    <dependency>
        <groupId>com.google.cloud</groupId>
        <artifactId>spring-cloud-gcp-starter-data-firestore</artifactId>
    </dependency>

and everything went to normal! Thanks to @Manoel Stilpen in this comment on this question here.

Incommunicable answered 10/2, 2024 at 20:41 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.