0

I am using two different databases for my spring hibernate application.

I defined two persistence config file for creating two session factory. When i start the servlet container it throws below exception

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'transactionManager' defined in class path resource

PersistenceConfig1

@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence.properties" })
@ComponentScan({ "com.test.test2.*" })
public class PersistenceConfiguration1 {

    @Autowired
    private Environment env;

    @Bean(name="fSessionFactory")
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(testDataSource());
        sessionFactory.setPackagesToScan(new String[] {
                " com.test.test2"});
        sessionFactory.setHibernateProperties(hibernateProperties1());

        return sessionFactory;
    }

    @Bean
    public DataSource testDataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc.url"));
        dataSource.setUsername(env.getProperty("jdbc.user"));
        dataSource.setPassword(env.getProperty("jdbc.pass"));

        return dataSource;
    }

    @Bean
    @Autowired
    public HibernateTransactionManager transactionManager(
            SessionFactory sessionFactory) {
        HibernateTransactionManager txManager = new HibernateTransactionManager();
        txManager.setSessionFactory(sessionFactory);

        return txManager;
    }

    @Bean
    public PersistenceExceptionTranslationPostProcessor exceptionTranslation() {
        return new PersistenceExceptionTranslationPostProcessor();
    }

    Properties hibernateProperties1() {
        return new Properties() {
            {
                setProperty("hibernate.hbm2ddl.auto",
                        env.getProperty("hibernate.hbm2ddl.auto"));
                setProperty("hibernate.dialect",
                        env.getProperty("hibernate.dialect"));
                setProperty("hibernate.globally_quoted_identifiers", "true");
            }
        };
    }
}

Persistence Config 2

@Configuration
@EnableTransactionManagement
@PropertySource({ "classpath:persistence.properties" })
@ComponentScan({ "com.test.test3.*" })
public class PersistenceConfigurationDOTC {

    @Autowired
    private Environment env;

    @Bean(name="dSessionFactory")
    public LocalSessionFactoryBean sessionFactory() {
        LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
        sessionFactory.setDataSource(test1DataSource());
        sessionFactory.setPackagesToScan(new String[] {
                "com.test.test3.entity"});
        sessionFactory.setHibernateProperties(hibernateProperties());

        return sessionFactory;
    }

    @Bean
    public DataSource test1DataSource() {
        BasicDataSource dataSource = new BasicDataSource();
        dataSource.setDriverClassName(env.getProperty("jdbc1.driverClassName"));
        dataSource.setUrl(env.getProperty("jdbc1.url"));
        dataSource.setUsername(env.getProperty("jdbc1.user"));
        dataSource.setPassword(env.getProperty("jdbc1.pass"));

        return dataSource;
    }


    Properties hibernateProperties() {
        return new Properties() {
            {
                setProperty("hibernate.hbm2ddl.auto",
                        env.getProperty("hibernate.hbm2ddl.auto"));
                setProperty("hibernate.dialect",
                        env.getProperty("hibernate.dialect"));
                setProperty("hibernate.globally_quoted_identifiers", "true");
            }
        };
    }
}

If I defined two transaction manager also im getting exception. Help me on how to configure two transaction managers in one spring hibernate application.

3

1 Answer 1

0

Do you want a single transaction manager for both the configs? Or would you prefer a multiple config? Try defining transaction manager in your Persistence Config 2 too.

Sign up to request clarification or add additional context in comments.

1 Comment

If i defined two transaction i am getting below exception No qualifying bean of type [org.hibernate.SessionFactory] is defined: expected single matching bean but found 2 session factory

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.