`

hibernate多数据源配置与使用

 
阅读更多

关于hibernate中多个数据源数据传输的使用:

1.首先需要配置文件:

oracle:oracle.cfg.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>

		<property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="hibernate.connection.url">jdbc:oracle:thin:@ip:1521:orcl</property>
		<property name="hibernate.connection.username">username</property>
		<property name="hibernate.connection.password">password</property>

		<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>

		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>

		<property name="hibernate.hbm2ddl.auto">none</property>

		<property name="hibernate.jdbc.fetch_size">100</property>
		<property name="hibernate.jdbc.batch_size">30</property>

		<!-- 配置二级缓存 -->
	 	<property name="hibernate.cache.use_second_level_cache">false</property>
	 	<property name="hibernate.cache.use_query_cache">false</property>
		<!-- Hibernate4 这里和Hibernate3不一样 要特别注意!!!-->
	 	<property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
		
		<!-- Hibernate3 -->
		<!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->
		
		<!-- 配置C3P0 -->
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<property name="hibernate.c3p0.max_size">10</property>
		<property name="hibernate.c3p0.min_size">1</property>
		<property name="hibernate.c3p0.max_statements">3</property>
		<property name="hibernate.c3p0.timeout">30</property>
		<property name="hibernate.c3p0.acquire_increment">1</property>
		<property name="hibernate.c3p0.idle_test_periodt">10</property>
		
		 <!-- <property name="current_session_context_class">thread</property>  -->
		 <mapping class="实体全路径"/>

	</session-factory>
</hibernate-configuration>

oracle使用hibernate4连接配置

public class OracleHibernateUtil {
	private static Logger logger = Logger.getLogger(OracleHibernateUtil.class);
	public static final String ORACLE_CFG = "oracle.cfg.xml";
	private static final SessionFactory sessionFactory ;
	private static Configuration config = null;
	public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
	
	static {
		config = new Configuration().configure(ORACLE_CFG);
		sessionFactory = config.buildSessionFactory(new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry());
	}

	/**
	 * 获取session
	 * @return
	 */
	public static Session currentSession() {
		Session s = session.get();
		if(s == null || !s.isOpen()) {
			s = sessionFactory.openSession();
			session.set(s);
		}
		if(s != null) {
			Transaction tran = null;
			try {
				tran = s.getTransaction();
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
			if(tran != null && !tran.isActive()) {
				tran.begin();
			}
		}
		
		return s;
	}
	
	public static void closeSession() {
		Session s = session.get();
		if(s != null && s.isOpen()) {
			Transaction tran = null;
			try {
				tran = s.getTransaction();
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
			if(tran != null && tran.isActive()) {
				tran.commit();
			} else {
				tran.rollback();
			}
		}
		session.set(null);
		if(s != null) {
			s.close();
		}
	}
	
	/**
	 * 提交
	 */
	public static void doCommit() {
		closeSession();
	}
	
	
}

 

 

2. sqlserver配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
		"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
	<session-factory>

		<property name="hibernate.connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
		<property name="hibernate.connection.url">jdbc:sqlserver://ip:1433;databaseName=databasename</property>
		<property name="hibernate.connection.username">username</property>
		<property name="hibernate.connection.password">password</property>

		<property name="hibernate.dialect">org.hibernate.dialect.SQLServer2008Dialect</property>

		<property name="hibernate.show_sql">true</property>
		<property name="hibernate.format_sql">true</property>

		<property name="hibernate.hbm2ddl.auto">none</property>

		<property name="hibernate.jdbc.fetch_size">100</property>
		<property name="hibernate.jdbc.batch_size">30</property>

		<!-- 配置二级缓存 -->
	 	<property name="hibernate.cache.use_second_level_cache">false</property>
	 	<property name="hibernate.cache.use_query_cache">false</property>
		<!-- Hibernate4 这里和Hibernate3不一样 要特别注意!!!-->
	 	<property name="hibernate.cache.region.factory_class">org.hibernate.cache.EhCacheRegionFactory</property>
		
		<!-- Hibernate3 -->
		<!-- <property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property> -->
		
		<!-- 配置C3P0 -->
		<property name="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
		<property name="hibernate.c3p0.max_size">10</property>
		<property name="hibernate.c3p0.min_size">1</property>
		<property name="hibernate.c3p0.max_statements">3</property>
		<property name="hibernate.c3p0.timeout">30</property>
		<property name="hibernate.c3p0.acquire_increment">1</property>
		<property name="hibernate.c3p0.idle_test_periodt">10</property>
		
		 <!-- <property name="current_session_context_class">thread</property>  -->
		 <mapping class="实体全路径名"/>
		
		 
	</session-factory>
</hibernate-configuration>

 hibernate4连接sqlserver2008

public class SqlServerHibernateUtil {
	private static Logger logger = Logger.getLogger(SqlServerHibernateUtil.class);
	public static final String DEST_CFG = "sqlserver.cfg.xml";
	private static final SessionFactory sessionFactory ;
	private static Configuration config = null;
	public static final ThreadLocal<Session> session = new ThreadLocal<Session>();
	
	static {
		config = new Configuration().configure(SQLSERVER_CFG);
		sessionFactory = config.buildSessionFactory(new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry());
	}

	/**
	 * 获取session
	 * @return
	 */
	public static Session currentSession() {
		Session s = session.get();
		if(s == null || !s.isOpen()) {
			s = sessionFactory.openSession();
			session.set(s);
		}
		if(s != null) {
			Transaction tran = null;
			try {
				tran = s.getTransaction();
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
			if(tran != null && !tran.isActive()) {
				tran.begin();
			}
		}
		return s;
	}
	
	public static void closeSession() {
		Session s = session.get();
		if(s != null && s.isOpen()) {
			Transaction tran = null;
			try {
				tran = s.getTransaction();
			} catch (Exception e) {
				logger.error(e.getMessage(), e);
			}
			if(tran != null && tran.isActive()) {
				tran.commit();
			} else {
				tran.rollback();
			}
		}
		session.set(null);
		if(s != null) {
			s.close();
		}
	}
	
	/**
	 * 提交
	 */
	public static void doCommit() {
		closeSession();
	}

}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics