上一篇文章介绍到了怎么样在Sping、MyBatis、Hibernate整合的应用中动态切换DataSource数据源的方法,但最终遗留下一个问题:不能切换数据库方言。数据库方言可能在当前应用的架构中意义不是很大,但是如果单纯用MyBatis或Hibernate做数据库持久化操作,还是要处理这一问题。
那么下面将介绍怎么样动态切换SessionFactory,为什么要切换SessionFactory?
因为这里切换SessionFactory就可以实现多数据源和多个SessionFactory,每个SessionFactory有自己独立的数据库配置和SessionFactory的相关配置。我们的数据库方言就配置在SessionFactory这里,所以实现了切换SessionFactory也就实现了切换数据库方言的问题。这个主要是针对Hibernate来操作的,而MyBatis则需要动态切换SqlSessionFactory才行。
1、定义全局切换SessionFactory的工具
com.hoo.framework.spring.support;<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000">/**</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * <b>function:</b> 多数据源</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @author hoojo</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @createDate 2013-9-27 上午11:36:57</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @file CustomerContextHolder.java</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @package com.hoo.framework.spring.support</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @project SHMB</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @blog http://blog.csdn.net/IBM_hoojo</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @email hoojo_@126.com</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @version 1.0</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> <span style="color: #0000ff">class</span> CustomerContextHolder {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">static</span> String SESSION_FACTORY_MYSQL = <span style="color: #006080">"mysql"</span>;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">static</span> String SESSION_FACTORY_ORACLE = <span style="color: #006080">"oracle"</span>;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> ThreadLocal<String> contextHolder = <span style="color: #0000ff">new</span> ThreadLocal<String>(); </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> setCustomerType(String customerType) { </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> contextHolder.set(customerType); </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> } </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> String getCustomerType() { </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> contextHolder.get(); </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">void</span> clearCustomerType() { </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> contextHolder.remove(); </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px">}</pre>
同样上面的静态变量和前一文章中介绍的一致,它需要和下面配置文件中的SessionFactory的key对应。
2、实现自己的SessionFactory
定义好接口
<p id="codeSnippetWrapper">
com.hoo.framework.spring.support.core;<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.SessionFactory;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * <b>function:</b> 动态SessionFactory接口</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @createDate 2013-10-12 下午03:29:52</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @file DynamicSessionFactory.java</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @package com.hoo.framework.spring.support.core</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">interface</span> DynamicSessionFactory <span style="color: #0000ff">extends</span> SessionFactory {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> SessionFactory getHibernateSessionFactory();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px">}</pre>
实现接口
<p id="codeSnippetWrapper">
java.io.Serializable;<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> java.sql.Connection;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> java.util.Map;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> java.util.Set;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> javax.naming.NamingException;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> javax.naming.Reference;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.Cache;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.HibernateException;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.Interceptor;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.SessionFactory;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.StatelessSession;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.TypeHelper;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.classic.Session;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.engine.FilterDefinition;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.metadata.ClassMetadata;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.metadata.CollectionMetadata;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.hibernate.stat.Statistics;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> com.hoo.framework.spring.support.CustomerContextHolder;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000">/**</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * <b>function:</b> 动态数据源实现</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @author hoojo</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @createDate 2013-10-12 下午03:31:31</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @file DynamicSessionFactoryImpl.java</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @package com.hoo.framework.spring.support.core</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @project SHMB</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @blog http://blog.csdn.net/IBM_hoojo</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @email hoojo_@126.com</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @version 1.0</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px">@SuppressWarnings({ <span style="color: #006080">"unchecked"</span>,<span style="color: #006080">"deprecation"</span> })</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DynamicSessionFactoryImpl <span style="color: #0000ff">implements</span> DynamicSessionFactory {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">long</span> serialVersionUID = 5384069312247414885L;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">private</span> Map<Object,SessionFactory> targetSessionFactorys; </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">private</span> SessionFactory defaultTargetSessionFactory; </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000">/**</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @see com.hoo.framework.spring.support.core.DynamicSessionFactory#getHibernateSessionFactory()</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * <b>function:</b> 重写这个方法,这里最关键</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @author hoojo</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @createDate 2013-10-18 上午10:45:25</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> @Override</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> SessionFactory getHibernateSessionFactory() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> SessionFactory targetSessionFactory = targetSessionFactorys.get(CustomerContextHolder.getCustomerType()); </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">if</span> (targetSessionFactory != null) { </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> targetSessionFactory; </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> } <span style="color: #0000ff">else</span> <span style="color: #0000ff">if</span> (defaultTargetSessionFactory != null) { </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> defaultTargetSessionFactory; </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> } </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> null;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> }</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> close() <span style="color: #0000ff">throws</span> HibernateException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().close();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> @Override</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> containsFetchProfileDefinition(String s) {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().containsFetchProfileDefinition(s);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> }</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> evict(Class clazz) <span style="color: #0000ff">throws</span> HibernateException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().evict(clazz);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> evict(Class clazz,Serializable serializable) <span style="color: #0000ff">throws</span> HibernateException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().evict(clazz,serializable);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> evictCollection(String s) <span style="color: #0000ff">throws</span> HibernateException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().evictCollection(s);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> evictCollection(String s,monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().evictCollection(s,monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> evictEntity(String entity) <span style="color: #0000ff">throws</span> HibernateException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().evictEntity(entity);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> evictEntity(String entity,monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().evictEntity(entity,monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> evictQueries() <span style="color: #0000ff">throws</span> HibernateException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().evictQueries(); </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> evictQueries(String queries) <span style="color: #0000ff">throws</span> HibernateException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().evictQueries(queries); </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> Map<String,ClassMetadata> getAllClassMetadata() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getAllClassMetadata();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> Map getAllCollectionMetadata() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getAllClassMetadata();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> Cache getCache() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getCache();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> ClassMetadata getClassMetadata(Class clazz) {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getClassMetadata(clazz);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> ClassMetadata getClassMetadata(String classMetadata) {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getClassMetadata(classMetadata);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> CollectionMetadata getCollectionMetadata(String collectionMetadata) {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getCollectionMetadata(collectionMetadata);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> Session getCurrentSession() <span style="color: #0000ff">throws</span> HibernateException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getCurrentSession();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> Set getDefinedFilterNames() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getDefinedFilterNames();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> FilterDefinition getFilterDefinition(String definition) <span style="color: #0000ff">throws</span> HibernateException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getFilterDefinition(definition);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> Statistics getStatistics() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getStatistics();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> TypeHelper getTypeHelper() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getTypeHelper();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">boolean</span> isClosed() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().isClosed();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> Session openSession() <span style="color: #0000ff">throws</span> HibernateException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().openSession();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> Session openSession(Interceptor interceptor) <span style="color: #0000ff">throws</span> HibernateException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().openSession(interceptor);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> Session openSession(Connection connection) {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().openSession(connection);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> Session openSession(Connection connection,Interceptor interceptor) {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().openSession(connection,interceptor);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> StatelessSession openStatelessSession() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().openStatelessSession();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> StatelessSession openStatelessSession(Connection connection) {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().openStatelessSession(connection);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> Reference getReference() <span style="color: #0000ff">throws</span> NamingException {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> <span style="color: #0000ff">this</span>.getHibernateSessionFactory().getReference();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setTargetSessionFactorys(Map<Object,SessionFactory> targetSessionFactorys) {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">this</span>.targetSessionFactorys = targetSessionFactorys;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> setDefaultTargetSessionFactory(SessionFactory defaultTargetSessionFactory) {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">this</span>.defaultTargetSessionFactory = defaultTargetSessionFactory;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px">}</pre>
上面最重要的就是getHibernateSessionFactory重写这个方法,其他方法和原来实现的无异。重写这个方法后利用CustomerContextHolder动态设置SessionFactory类型就可以动态的切换SessionFactory。
3、动态的事务管理器,因为我们这里是动态切换SessionFactory,所以事务这块也需要动态切换SessionFactory来完成事务的操作。
<p id="codeSnippetWrapper">
com.hoo.framework.spring.support.tx;<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> javax.sql.DataSource;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.springframework.orm.hibernate3.HibernateTransactionManager;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> org.springframework.orm.hibernate3.SessionFactoryUtils;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">import</span> com.hoo.framework.spring.support.core.DynamicSessionFactory;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * <b>function:</b> 重写HibernateTransactionManager事务管理器,实现自己的动态的事务管理器</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @createDate 2013-10-12 下午03:54:02</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @file DynamicTransactionManager.java</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @package com.hoo.framework.spring.support.tx</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> DynamicTransactionManager <span style="color: #0000ff">extends</span> HibernateTransactionManager {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">private</span> <span style="color: #0000ff">static</span> <span style="color: #0000ff">final</span> <span style="color: #0000ff">long</span> serialVersionUID = -4655721479296819154L;</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000">/** </span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @see org.springframework.orm.hibernate4.HibernateTransactionManager#getDataSource()</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * <b>function:</b> 重写</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @author hoojo</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @createDate 2013-10-12 下午03:55:24</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> */</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> DataSource getDataSource() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> SessionFactoryUtils.getDataSource(getSessionFactory());</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000">/** </span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @see org.springframework.orm.hibernate4.HibernateTransactionManager#getSessionFactory()</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * <b>function:</b> 重写</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #008000"> * @createDate 2013-10-12 下午03:55:24</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">public</span> SessionFactory getSessionFactory() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> DynamicSessionFactory dynamicSessionFactory = (DynamicSessionFactory) <span style="color: #0000ff">super</span>.getSessionFactory(); </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> SessionFactory hibernateSessionFactory = dynamicSessionFactory.getHibernateSessionFactory(); </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">return</span> hibernateSessionFactory; </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px">}</pre>
这里主要重写getDataSource()/getSessionFactory()这两个方法,getSessionFactory方法是利用我们上面定义的接口来动态获取我们在上下文(CustomerContextHolder)中定义切换的SessionFactory对象。而getDataSource则是获得动态SessionFactory的DataSource,这里也不难理解。
4、至此,重写的接口和实现都完成,下面开始配置相关的代码
<p id="codeSnippetWrapper">
?<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff"><</span><span style="color: #800000">beans</span> <span style="color: #ff0000">xmlns</span><span style="color: #0000ff">="http://www.springframework.org/schema/beans"</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #ff0000">xmlns:aop</span><span style="color: #0000ff">="http://www.springframework.org/schema/aop"</span> </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #ff0000">xmlns:tx</span><span style="color: #0000ff">="http://www.springframework.org/schema/tx"</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #ff0000">xmlns:xsi</span><span style="color: #0000ff">="http://www.w3.org/2001/XMLSchema-instance"</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #ff0000">xsi:schemaLocation</span><span style="color: #0000ff">="http://www.springframework.org/schema/beans </span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> http://www.springframework.org/schema/beans/spring-beans-3.2.xsd </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> http://www.springframework.org/schema/aop </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> http://www.springframework.org/schema/aop/spring-aop-3.2.xsd </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> http://www.springframework.org/schema/tx </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "<span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000"><!-- 配置c3p0数据源 --></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">bean</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">="dataSourceOracle"</span> <span style="color: #ff0000">class</span><span style="color: #0000ff">="com.mchange.v2.c3p0.ComboPooledDataSource"</span> <span style="color: #ff0000">destroy-method</span><span style="color: #0000ff">="close"</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="driverClass"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${datasource.driver}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="jdbcUrl"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${datasource.url}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="user"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${datasource.username}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="password"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${datasource.password}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="acquireIncrement"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${c3p0.acquireIncrement}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="initialPoolSize"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${c3p0.initialPoolSize}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="minPoolSize"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${c3p0.minPoolSize}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="maxPoolSize"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${c3p0.maxPoolSize}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="maxIdleTime"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${c3p0.maxIdleTime}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="idleConnectionTestPeriod"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${c3p0.idleConnectionTestPeriod}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="maxStatements"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${c3p0.maxStatements}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="numHelperThreads"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${c3p0.numHelperThreads}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="preferredTestQuery"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${c3p0.preferredTestQuery}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="testConnectionOnCheckout"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="${c3p0.testConnectionOnCheckout}"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">bean</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">="dataSourceMySQL"</span> <span style="color: #ff0000">class</span><span style="color: #0000ff">="com.mchange.v2.c3p0.ComboPooledDataSource"</span> <span style="color: #ff0000">destroy-method</span><span style="color: #0000ff">="close"</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="driverClass"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="com.mysql.jdbc.Driver"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="jdbcUrl"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="jdbc:mysql://172.31.108.178:3306/world?useUnicode=true&amp;characterEncoding=UTF-8&amp;zeroDateTimeBehavior=convertToNull"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="user"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="root"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="password"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="jp2011"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000"><!-- Annotation 配置sessionFactory,配置数据库连接,注入hibernate数据库配置 --></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">bean</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">="mySQLSessionFactory"</span> <span style="color: #ff0000">class</span><span style="color: #0000ff">="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="dataSource"</span> <span style="color: #ff0000">ref</span><span style="color: #0000ff">="dataSourceMySQL"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="packagesToScan"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="com.hoo.**.mysqlentity"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="annotatedClasses"</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">array</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">value</span><span style="color: #0000ff">></span>com.hoo.common.entity.IDGenerator<span style="color: #0000ff"></</span><span style="color: #800000">value</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"></</span><span style="color: #800000">array</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"></</span><span style="color: #800000">property</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="hibernateProperties"</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">props</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">prop</span> <span style="color: #ff0000">key</span><span style="color: #0000ff">="hibernate.dialect"</span><span style="color: #0000ff">></span>org.hibernate.dialect.MySQLDialect<span style="color: #0000ff"></</span><span style="color: #800000">prop</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000"><!-- 链接释放策略 on_close | after_transaction | after_statement | auto --></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">prop</span> <span style="color: #ff0000">key</span><span style="color: #0000ff">="hibernate.connection.release_mode"</span><span style="color: #0000ff">></span>after_transaction<span style="color: #0000ff"></</span><span style="color: #800000">prop</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">prop</span> <span style="color: #ff0000">key</span><span style="color: #0000ff">="hibernate.show_sql"</span><span style="color: #0000ff">></span>true<span style="color: #0000ff"></</span><span style="color: #800000">prop</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">prop</span> <span style="color: #ff0000">key</span><span style="color: #0000ff">="hibernate.format_sql"</span><span style="color: #0000ff">></span>true<span style="color: #0000ff"></</span><span style="color: #800000">prop</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"></</span><span style="color: #800000">props</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"></</span><span style="color: #800000">property</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">bean</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">="oracleSessionFactory"</span> <span style="color: #ff0000">class</span><span style="color: #0000ff">="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="dataSource"</span> <span style="color: #ff0000">ref</span><span style="color: #0000ff">="dataSourceOracle"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="packagesToScan"</span> <span style="color: #ff0000">value</span><span style="color: #0000ff">="com.hoo.**.entity"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="hibernateProperties"</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">props</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">prop</span> <span style="color: #ff0000">key</span><span style="color: #0000ff">="hibernate.dialect"</span><span style="color: #0000ff">></span>org.hibernate.dialect.OracleDialect<span style="color: #0000ff"></</span><span style="color: #800000">prop</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000"><!--prop key="hibernate.hbm2ddl.auto">update</prop--></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"></</span><span style="color: #800000">props</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"></</span><span style="color: #800000">bean</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000"><!-- 动态SessionFactory --></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">bean</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">="sessionFactory"</span> <span style="color: #ff0000">class</span><span style="color: #0000ff">="com.hoo.framework.spring.support.core.DynamicSessionFactoryImpl"</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="defaultTargetSessionFactory"</span> <span style="color: #ff0000">ref</span><span style="color: #0000ff">="oracleSessionFactory"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="targetSessionFactorys"</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">map</span><span style="color: #0000ff">></span> </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">entry</span> <span style="color: #ff0000">value-ref</span><span style="color: #0000ff">="oracleSessionFactory"</span> <span style="color: #ff0000">key</span><span style="color: #0000ff">="oracle"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">entry</span> <span style="color: #ff0000">value-ref</span><span style="color: #0000ff">="mySQLSessionFactory"</span> <span style="color: #ff0000">key</span><span style="color: #0000ff">="mysql"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"></</span><span style="color: #800000">map</span><span style="color: #0000ff">></span> </pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000"><!-- 自定义动态切换SessionFactory事务管理器,注入sessionFactory --></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">bean</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">="transactionManager"</span> <span style="color: #ff0000">class</span><span style="color: #0000ff">="com.hoo.framework.spring.support.tx.DynamicTransactionManager"</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">property</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="sessionFactory"</span> <span style="color: #ff0000">ref</span><span style="color: #0000ff">="sessionFactory"</span> <span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000"><!-- 配置事务的传播特性 --></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">tx:advice</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">="txAdvice"</span> <span style="color: #ff0000">transaction-manager</span><span style="color: #0000ff">="transactionManager"</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">tx:attributes</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">tx:method</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="add*"</span> <span style="color: #ff0000">propagation</span><span style="color: #0000ff">="REQUIRED"</span> <span style="color: #ff0000">rollback-for</span><span style="color: #0000ff">="java.lang.Exception"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">tx:method</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="edit*"</span> <span style="color: #ff0000">propagation</span><span style="color: #0000ff">="REQUIRED"</span> <span style="color: #ff0000">rollback-for</span><span style="color: #0000ff">="java.lang.Exception"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">tx:method</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="remove*"</span> <span style="color: #ff0000">propagation</span><span style="color: #0000ff">="REQUIRED"</span> <span style="color: #ff0000">rollback-for</span><span style="color: #0000ff">="java.lang.Exception"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">tx:method</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="execute*"</span> <span style="color: #ff0000">propagation</span><span style="color: #0000ff">="REQUIRED"</span> <span style="color: #ff0000">rollback-for</span><span style="color: #0000ff">="java.lang.Exception"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">tx:method</span> <span style="color: #ff0000">name</span><span style="color: #0000ff">="*"</span> <span style="color: #ff0000">read-only</span><span style="color: #0000ff">="true"</span> <span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"></</span><span style="color: #800000">tx:attributes</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"></</span><span style="color: #800000">tx:advice</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000"><!-- 配置那些类、方法纳入到事务的管理 --></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">aop:config</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">aop:pointcut</span> <span style="color: #ff0000">expression</span><span style="color: #0000ff">="execution(* com.hoo.**.service.impl.*.*(..))"</span> <span style="color: #ff0000">id</span><span style="color: #0000ff">="transactionManagerMethod"</span><span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"><</span><span style="color: #800000">aop:advisor</span> <span style="color: #ff0000">advice-ref</span><span style="color: #0000ff">="txAdvice"</span> <span style="color: #ff0000">pointcut-ref</span><span style="color: #0000ff">="transactionManagerMethod"</span> <span style="color: #0000ff">/></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff"></</span><span style="color: #800000">aop:config</span><span style="color: #0000ff">></span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff"></</span><span style="color: #800000">beans</span><span style="color: #0000ff">></span></pre>
配置也和我们之前的配置差不多,就是事务管理器部分注入的SessionFactory是我们自己定义的。
5、简单测试
<p id="codeSnippetWrapper">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> testAdd() {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #008000">// 主要就是这行代码 它才是完成SessionFactory的切换</span></pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> CustomerContextHolder.setCustomerType(CustomerContextHolder.SESSION_FACTORY_MYSQL);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> DeviceInfo entity = <span style="color: #0000ff">new</span> DeviceInfo();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> entity.setSbbh(System.currentTimeMillis() + <span style="color: #006080">""</span>);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> entity.setIpdz(<span style="color: #006080">"my ip address2"</span>);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> entity.setJd(1234);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> <span style="color: #0000ff">try</span> {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> service.add(entity);</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> } <span style="color: #0000ff">catch</span> (Exception e) {</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px"> e.printStackTrace();</pre>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New',monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 10pt; overflow: visible; padding-top: 0px">}</pre>
经过测试发现可以查询数据,利用hibernate查询分页也可以生成对应数据库的分页语句。同样在服务层的方法中添加完操作后,特意抛出一个异常,数据也能正常回滚操作,所以DynamicTransactionManager也是起到了该有的作用!
这里我的测试用例是手动切换CustomerContextHolder.setCustomerType的,但实际开发中我们还是得用Spring的Aop中的Interceptor进行切面编程,完成动态切换SessionFactory。上一篇文章已经提到了(读者可以参考该篇博文中的第二节的3、7小节DataSourceMethodInterceptor MultipleDataSourceInterceptor),这里就不再赘述!
上述的实现如果在使用不当的情况下,在实际开发中很可能存在一些问题!
问题1是这样的,通常事务是在Service这层完成,这个应该是没有异议。倘若是这样的话,问题便出现了。而通常我们在MVC中的视图层中仅仅会调用Service中一个方法来完成所有当前业务的处理。如果这个Service中同时操作dbA、dbB两个数据库,事务提交的时候是用哪个数据库的事务呢?数据库的操作放在一个方法,就会出现事务的问题的,除非两个数据库的事务能够同时回滚!
数据库,Service中的add4MysqL操作MysqL数据库,最后把Service中的add4Oracle、add4MysqL方法放到一个operation方法中,MVC视图层的业务控制调用Service中的operation。像这样的情况,如果add4Oracle或add4MysqL方法中的某一个方法出现异常,就需要把两个数据库事务都回滚。
解决办法就是在Service或Dao中的一个方法中同时操作两个数据库,手动完成事务控制。出现异常就全部回滚,没有异常就全部提交,只能这样牺牲下。
拦截器不能同时切换一个方法操作两个数据库的可能,例如一个service中的query方法即需要用query MysqL,也需要query Oracle。那么解决办法就是在query方法中调用当前service的query4Oracle和query4Oracle,那样绕过去还是能利用拦截器进行动态切换的。
四、总结
数据库方言的切换,我们就需要配置多个SessionFactory利用自己实现的DynamicSessionFactory和CustomerContextHolder完成SessionFactory的切换。利用DynamicTransactionManager完成当前Session的事务操作。通过对这些对象的操作和配置,最终可以完成SessionFactory的动态切换。实际使用中虽然有些问题出现,但是最终还是有解决方案,尽管有些不完美。所谓的完美的东西总有它不完美的地方,这样才算完美,比如断臂的维纳斯女神~!