java spring中的多个DB配置会引发异常

时间:2016-05-18 17:17:23

标签: java spring hibernate transactions

我在spring spring hibernate应用程序中使用了两个不同的数据库。

我定义了两个持久性配置文件来创建两个会话工厂。 当我启动servlet容器时,它会抛出异常

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");
            }
        };
    }
}

持久性配置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");
            }
        };
    }
}

如果我定义了两个事务管理器,我也会获得异常。 帮助我了解如何在一个Spring hibernate应用程序中配置两个事务管理器。

1 个答案:

答案 0 :(得分:0)

你想要两个配置的单个事务管理器吗?或者你更喜欢多配置?尝试在Persistence Config 2中定义事务管理器。