0

I want to connect to two servers on hibernate. But i am using java class as configuration. How give Session factory know it will use connection one or connection two.

this is my class:

    @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;
    }
}

i want to add another connection :

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

how to give information to SessionFactory using one connection or another connection?

this is example using session factory:

@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);
    }
}
user3505775
  • 339
  • 2
  • 6
  • 20

1 Answers1

0

Using annotation mappings as an example:

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();

Then use sf1 and sf2 to get the sessions for each database. For mapping files, you just use cfg.addClass instead of addAnnotatedClass. Put the cfg.xml files in the root package in this case. Those will have the Oracle or MySQL dialect and connection information.

P S M
  • 1,121
  • 12
  • 29
  • but i have a big project and the configuration can't be changed, so i can't change from java class configuration into xml configuration. do you have an idea to use java class configuration and how to tell to sessionfactory? – user3505775 Apr 22 '16 at 11:08