Hibernate源代码分析(二):设计属于我的SessionFactory和ConnectionProvider

http://www.blogjava.net/awp001/archive/2007/05/20/118626.html

 

分析到了SessionFactoryImpl.openSession()方法,该方法将其
职责委托给了SessionImpl,打开org.hibernate.impl.SessionImpl.java,看看实现代码:

 1     SessionImpl(
 2              final  Connection connection,
 3              final  SessionFactoryImpl factory,
 4              final   boolean  autoclose,
 5              final   long  timestamp,
 6              final  Interceptor interceptor,
 7              final  EntityMode entityMode,
 8              final   boolean  flushBeforeCompletionEnabled,
 9              final   boolean  autoCloseSessionEnabled,
10              final  ConnectionReleaseMode connectionReleaseMode)  {
11        super( factory );
12        this.rootSession = null;
13        this.timestamp = timestamp;
14        this.entityMode = entityMode;
15        this.interceptor = interceptor;
16        this.listeners = factory.getEventListeners();
17        this.actionQueue = new ActionQueue( this );
18        this.persistenceContext = new StatefulPersistenceContext( this );
19        this.flushBeforeCompletionEnabled = flushBeforeCompletionEnabled;
20        this.autoCloseSessionEnabled = autoCloseSessionEnabled;
21        this.connectionReleaseMode = connectionReleaseMode;
22        this.jdbcContext = new JDBCContext( this, connection, interceptor );
23
24        if ( factory.getStatistics().isStatisticsEnabled() ) {
25            factory.getStatisticsImplementor().openSession();
26        }

27
28        if ( log.isDebugEnabled() ) {
29            log.debug( "opened session at timestamp: " + timestamp );
30        }

31    }

 


      我们关注的就是数据库连接,重点关注第22行,在该行将Connection对象传递给了JDBCContext,通过观察前面的代码我们可以发现,JTASessionContext.buildOrObtainSession() 方法传递的Connection对象为null,这个可以说,Connection对象的获得将由
ConnectionProvider接口的实现类来完成。

      接下来看看JDBCContent类的构造函数,跳转到org.hibernate.jdbc.JDBCContent.java,程序代码如下:

 

 1      public  JDBCContext(Context owner, Connection connection, Interceptor interceptor)  {
 2        this.owner = owner;
 3        this.connectionManager = new ConnectionManager(
 4                owner.getFactory(),
 5                this,
 6                owner.getConnectionReleaseMode(),
 7                connection,
 8                interceptor
 9            );
10
11        final boolean registerSynchronization = owner.isAutoCloseSessionEnabled()
12                || owner.isFlushBeforeCompletionEnabled()
13                || owner.getConnectionReleaseMode() == ConnectionReleaseMode.AFTER_TRANSACTION;
14        if ( registerSynchronization ) {
15            registerSynchronizationIfPossible();
16        }

17    }


      这段程序代码里创建了一个新的对象connectionManager,我们跟踪一下org.hibernate.jdbc.ConnectionManager.java,在这个文件里可以
发现一个方法openConnection(),代码如下:

 1      private   void  openConnection()  throws  HibernateException  {
 2        if ( connection != null ) {
 3            return;
 4        }

 5
 6        log.debug("opening JDBC connection");
 7        try {
 8            connection = factory.getConnectionProvider().getConnection();
 9        }

10        catch (SQLException sqle) {
11            throw JDBCExceptionHelper.convert(
12                    factory.getSQLExceptionConverter(),
13                    sqle,
14                    "Cannot open connection"
15                );
16        }

17
18        callback.connectionOpened(); // register synch; stats.connect()
19    }


      现在,最有价值的发现出来了,在第8行,这里就是从缓冲池获得数据库连接的代码。

      接下来我们可以设计实现ConnectionProvider的具体类了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值