0

我想在休眠状态下连接到两台服务器。但我使用 java 类作为配置。如何让会话工厂知道它将使用连接一或连接二。

这是我的课:

    @Configuration
@ComponentScan(basePackages = {"id.bni.hcms"})
@EnableTransactionManagement
public class RepositoryConfig {

        @Bean(name = "dataSource")
    public DataSource initDataSource(){
        System.out.println("datasource inited initDataSource" );
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException ex) {

        }
        String url = "jdbc:oracle:thin:@//xxx.xxx.xx.xxx:xxx/xxxx";
        String username = "xxx";
        String password = "xxxx";

        ConnectionFactory cf = new DriverManagerConnectionFactory(url, username, password);
        PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, null);
        GenericObjectPool<PoolableConnection> cp = new GenericObjectPool<>(pcf);

        pcf.setPool(cp);

        return new PoolingDataSource<>(cp);
    }

    @Bean(name = "sessionFactory")
    public LocalSessionFactoryBean sessionFactoryBean(DataSource dataSource){
        LocalSessionFactoryBean sfb = new LocalSessionFactoryBean();
        sfb.setDataSource(dataSource);
        sfb.setPackagesToScan("id.bni.hcms");

        Properties props = sfb.getHibernateProperties();

        props.put("hibernate.format_sql", true);
        props.put("hibernate.show_sql", true);
        props.put("hibernate.dialect", "org.hibernate.dialect.Oracle9iDialect"); 

        return sfb;
    }

    @Bean(name = "transactionManager")
    public PlatformTransactionManager getHibernateTransactionManager(SessionFactory sessionFactory){
        HibernateTransactionManager trxMgr = new HibernateTransactionManager(sessionFactory);

        return trxMgr;
    }
}

我想添加另一个连接:

String url = "jdbc:oracle:thin:@//yyyy:yyyy/yyyy";
    String username = "yyyy";
    String password = "yyyy";

如何使用一个连接或另一个连接向 SessionFactory 提供信息?

这是使用会话工厂的示例:

@Repository
public class KaryawanDaoImpl implements KaryawanDao{
    @Autowired
    private SessionFactory sessionFactory;

    @Override
    @Transactional(propagation = Propagation.REQUIRED)
    public Karyawan find(String username, String password) {
            String hql = " SELECT * from table";
            Query query = sessionFactory.getCurrentSession().createSQLQuery(hql).addEntity(Karyawan.class);
            query.setString("user", username);
            query.setString("pwd", password);
            Karyawan result = (Karyawan)query.uniqueResult();

            return result;
    }

    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = {Exception.class})
    public void save(Karyawan e) {
        sessionFactory.getCurrentSession()
            .saveOrUpdate(e);
    }
}
4

1 回答 1

0

以注解映射为例:

Configuration cfg1 = new AnnotationConfiguration();
cfg1.configure("/hibernate-oracle.cfg.xml");
cfg1.addAnnotatedClass(SomeClass.class); // mapped classes
cfg1.addAnnotatedClass(SomeOtherClass.class);
SessionFactory sf1 = cfg1.buildSessionFactory();

Configuration cfg2 = new AnnotationConfiguration();
cfg2.configure("/hibernate-mysql.cfg.xml");
cfg2.addAnnotatedClass(SomeClass.class); // could be the same or different than above
cfg2.addAnnotatedClass(SomeOtherClass.class);
SessionFactory sf2 = cfg2.buildSessionFactory();

然后使用 sf1 和 sf2 获取每个数据库的会话。对于映射文件,您只需使用 cfg.addClass 而不是 addAnnotatedClass。在这种情况下,将 cfg.xml 文件放在根包中。这些将具有 Oracle 或 MySQL 方言和连接信息。

于 2016-04-22T11:05:15.650 回答