欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

Hibernate源代碼分析(一):設計屬于我的Session

系統 1657 0
過完五一長假,花了四天的時間來學習Hibernate框架的使用,作為門外漢,先是從sourceforg.net下載 Hibernate3.2,先看官方文檔,只有一個提綱,了解了一下各個包的結構,便開始根據Toturial實踐,基本掌握了它的使用方法之后,盟生了 實現自己的SessionFactory和ConnectionProvider的想法。
閑話少說,要實現我的SessionFactory和ConnectionProvider,不深入了解該體系結構是不行的,先從源代碼分析開始: 首先從org.hibernate.cfg.Configuration.java開始,使用Hibernate框架實現應用程序,首先就要與org.hibernate.cfg.Configuration打交道,要使用
Configuration.buildSessionFactory()方法獲得一個SessionFactory,截取代碼片段如下:

1 public SessionFactorybuildSessionFactory() throws HibernateException {
2
3 return new SessionFactoryImpl(
4 this ,
5 mapping,
6 settings,
7 getInitializedEventListeners()
8 );
9 }

上面的代碼片斷省略了讀取Hibernate配置的代碼,從這個我們可以知道,Configuration類buildSessionFactory()方法實際上返回了SessionFactory接口的實現SessionFactoryImpl。

當我們得到了一個SessionFactory接口的實現SessionFactoryImpl,就要調用它的getCurrentSession()方 法來獲得一個Session,接下來轉到org.hibernate.impl.SessionFactoryImpl.java,來看看 getCurrentSession()方法的實現,代碼片段如下:
1 public org.hibernate.classic.SessiongetCurrentSession() throws HibernateException {
2 if (currentSessionContext == null ) {
3 throw new HibernateException( " NoCurrentSessionContextconfigured! " );
4 }

5 return currentSessionContext.currentSession();
6 }

在該方法中,SessionFactoryImpl將獲得Session的工作委托給了currentSessionContext.currentSession(),currentSessionContext為何物?其定義為:
org.hibernate.context.CurrentSessionContext;在SessionFactoryImpl的構造函數中,可以看到:

currentSessionContext = buildCurrentSessionContext();

馬上追溯到buildCurrentSessionContext()方法,代碼片段如下:

1 private CurrentSessionContextbuildCurrentSessionContext() {
2 Stringimpl = properties.getProperty(Environment.CURRENT_SESSION_CONTEXT_CLASS);
3 // forbackward-compatability
4 if (impl == null && transactionManager != null ) {
5 impl = " jta " ;
6 }

7
8 if (impl == null ) {
9 return null ;
10 }

11 else if ( " jta " .equals(impl)) {
12 if (settings.getTransactionFactory().areCallbacksLocalToHibernateTransactions()) {
13 log.warn( " JTASessionContextbeingusedwithJDBCTransactionFactory;auto-flushwillnotoperatecorrectlywithgetCurrentSession() " );
14 }

15 return new JTASessionContext( this );
16 }

17 else if ( " thread " .equals(impl)) {
18 return new ThreadLocalSessionContext( this );
19 }

20 else if ( " managed " .equals(impl)) {
21 return new ManagedSessionContext( this );
22 }

23 else {
24 try {
25 ClassimplClass = ReflectHelper.classForName(impl);
26 return (CurrentSessionContext)implClass
27 .getConstructor( new Class[] {SessionFactoryImplementor. class } )
28 .newInstance( new Object[] { this } );
29 }

30 catch (Throwablet) {
31 log.error( " Unabletoconstructcurrentsessioncontext[ " + impl + " ] " ,t);
32 return null ;
33 }

34 }

35 }

36

從這里可以發現,SessionFactoryImpl用反射實現了CurrentSessionContext接口的動態裝配。
接下來,暫時將視線從SessionFactoryImpl移開,以org.hibernate.context.JTASessionContext為代表,看CurrentSessionContext接口是如何實現
的currentSession()方法的,打開org.hibernate.context.JTASessionContext.java,找到currentSession(),代碼片段如下:

1 public SessioncurrentSession() throws HibernateException {
2
3 if (currentSession == null ) {
4 currentSession = buildOrObtainSession();
5
6 }

7
8 return currentSession;
9 }

轉移到buildOrObtainSession()方法,

1 protected SessionbuildOrObtainSession() {
2 return factory.openSession(
3 null ,
4 isAutoFlushEnabled(),
5 isAutoCloseEnabled(),
6 getConnectionReleaseMode()
7 );
8 }

前面SessionFactoryImpl.buildCurrentSessionContext()方法有new JTASessionContext( this ),而此時的factory.openSessio()就是SessionFactoryImpl.openSessio()了。

將目光焦點回到org.hibernate.impl.SessionFactoryImpl.java,SessionFactoryImpl.openSessio()的實現如下:

1 private SessionImplopenSession(
2 Connectionconnection,
3 boolean autoClose,
4 long timestamp,
5 InterceptorsessionLocalInterceptor
6 ) {
7 return new SessionImpl(
8 connection,
9 this ,
10 autoClose,
11 timestamp,
12 sessionLocalInterceptor == null ? interceptor:sessionLocalInterceptor,
13 settings.getDefaultEntityMode(),
14 settings.isFlushBeforeCompletionEnabled(),
15 settings.isAutoCloseSessionEnabled(),
16 settings.getConnectionReleaseMode()
17 );
18 }

Hibernate源代碼分析(一):設計屬于我的SessionFactory和ConnectionProvider


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 精品一区二区三区免费视频 | 野花国产精品入口 | 国产在线视频一区二区 | 波多在线视频 | 精产国产伦理一二三区 | 夜色亚洲 | 国产免费中文字幕 | 中文字幕一区二区三区四区五区 | 久久网精品视频 | 久久夏同学国产免费观看 | 色综合天天综合网国产成人网 | 日韩精品免费视频 | 日本AAAA片毛片免费观 | 黄色特级一级片 | 色偷偷888欧美精品久久久 | 国产精品秒播无毒不卡 | 久久久亚洲欧洲日产国码606 | 看个毛片| 亚洲成人免费网站 | 夜夜撸天天操 | 免费综合网 | 毛片国产 | 国产成人91高清精品免费 | 天天天天天天操 | 亚洲网站免费观看 | 色呦呦在线观看视频 | 亚洲天堂日本 | 日本高清视频www夜色资源网 | 成人区视频爽爽爽爽爽 | 在线观看毛片视频 | 日本不卡在线视频 | 中文字幕亚洲一区二区三区 | 亚洲人在线 | 亚洲精品美女视频 | 欧美午夜不卡 | 亚洲国产日韩在线观看 | 美美女高清毛片视频免费观看 | 欧美日本一道高清二区三区 | 欧美亚洲国产另类在线观看 | 免费一区二区三区 | 国产毛片不卡 |