在Hibernate中连接多个数据库

时间:2011-10-10 03:08:37

标签: java mysql hibernate

如何使用Hibernate动态连接到多个mysql数据库?

3 个答案:

答案 0 :(得分:5)

我相信在Spring中你可以使用一个单独的DataSource来拥有多个SessionFactories。您可以将特定的SessionFactory传递给适当的DAO。

答案 1 :(得分:3)

如果你使用带有spring的hibernate,你可以为每个数据库提供多个hibernate属性xml。在这些xml文件中,您可以指定数据库主机,用户名,密码,数据库名称和其他连接属性。您可以使用xml文件创建多个会话工厂,并在DAO类中使用正确的会话工厂。

答案 2 :(得分:1)

创建一个包含HibernateProperties和SessionFactory实体的类。 类似的东西:

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;

如果您以后可以使用Spring并使用数据库连接详细信息注入此类,并稍后将此HibernateSessionFactory分配给相关的DAO类,那将是很好的。

我之前做过,或多或少看起来像这样:

<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>

虽然不知道如何使用注释。