Connect to multiple databases in sleep mode

How can I connect to multiple mysql databases dynamically using Hibernate?

+2
source share
3 answers

I believe in Spring, you can have several SessionFactories, each of which uses a separate DataSource. You can pass a specific SessionFactory to the corresponding DAO.

+5
source

If you use hibernation without spring, you can have several hibernate xml properties for each database. In these xml files you can specify the database host, username, password, database name and other connection properties. You can create multiple session factories using xml files and use the correct factory session in DAO classes.

+3
source

Create a class containing the HibernateProperties and SessionFactory objects. Sort of:

public class HibernateSessionFactory { private static final long serialVersionUID = 1L; private static Log log = LogFactory.getLog(HibernateSessionFactory.class); private Resource[] mappingLocations; private Properties hibernateProperties; private SessionFactory factory; 

Well, if you can use Spring later and introduce this class with database connection details, then assign this HibernateSessionFactory to the corresponding DAO classes.

I have done this before, more or less it looks like this:

 <bean id="mapHibernateFactory" class="com.geofencing.dao.hibernate.HibernateSessionFactory" init-method="init" destroy-method="dispose" scope="singleton"> <property name="mappingResources"> <list> ..... your hibernate mapping files ..... </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.hbm2ddl.auto">false</prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.connection.isolation">4</prop> <prop key="hibernate.connection.autocommit">false</prop> <prop key="hibernate.connection.url">${jdbc.url}</prop> <prop key="hibernate.connection.username">${jdbc.username}</prop> <prop key="hibernate.connection.password">${jdbc.password}</prop> <prop key="hibernate.connection.driver_class">${jdbc.driverClassName}</prop> <prop key="hibernate.dialect">${jdbc.dialect}</prop> <prop key="hibernate.c3p0.min_size">5</prop> <prop key="hibernate.c3p0.max_size">20</prop> <prop key="hibernate.c3p0.timeout">1800</prop> <prop key="hibernate.c3p0.max_statements">50</prop> <prop key="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider</prop> <prop key="net.sf.ehcache.configurationResourceName">WEB-INF/ehcache.xml</prop> <prop key="hibernate.cglib.use_reflection_optimizer">false</prop> <prop key="hibernate.connection.zeroDateTimeBehavior">convertToNull</prop> <prop key="hibernate.connection.autoReconnect">true</prop> <prop key="hibernate.connection.autoReconnectForPools">true</prop> </props> </property> </bean> 

I don’t know how to do this with annotation.

+1
source

All Articles