hibernate一些底层实现的解析
来源:互联网 发布:钢铁雄心4多核优化补丁 编辑:程序博客网 时间:2024/06/14 07:35
一、SessionFactory.openSession()
1、获取Connection
这里会通过调用hibernate的ConnectionProvider接口的getConnection()获取Connection,而实际执行的有三个类继承ConnectionProvider:
1、DatasourceConnectionProviderImpl通过数据源获取:dataSource.getConnection();
2、DriverManagerConnectionProviderImpl通过ConnectionCreator获取:connectionCreator.createConnection();
3、UserSuppliedConnectionProviderImpl仅仅是抛出异常;
这里详细说明一下第一种:hibernate和各种连接池的联系是通过java的DateSource,hibernate使用DateSource的接口,各种连接池提供DateSource的实现类。而我们则是把该实现类定义为Bean。比如在tomcat.jdbc.pool中,dataSource.getConnection()这个方法实际上是通过连接池的ConnectionPool的ProxyClassConstructor.newInstance()获取Connection。
2、openSession()的实现代码(hibernate),简略:
package org.hibernate.internal;import java.sql.Connection;import org.hibernate.Interceptor;import org.hibernate.Session;import org.hibernate.SessionEventListener;import org.hibernate.engine.spi.SessionBuilderImplementor;import org.hibernate.engine.spi.SessionFactoryImplementor;import org.hibernate.engine.spi.SessionOwner;import org.hibernate.internal.SessionImpl;public final class SessionFactoryImpl implements SessionFactoryImplementor {static class SessionBuilderImpl implements SessionBuilderImplementor {private final SessionFactoryImpl sessionFactory;private Connection connection;@Overridepublic Session openSession() {final SessionImpl session = new SessionImpl(connection,sessionFactory,sessionOwner,getTransactionCoordinator(),getTransactionCompletionProcesses(),autoJoinTransactions,sessionFactory.settings.getRegionFactory().nextTimestamp(),interceptor,flushBeforeCompletion,autoClose,connectionReleaseMode,tenantIdentifier);for ( SessionEventListener listener : listeners ) {session.getEventListenerManager().addListener( listener );}return session;}}}因此有时候我们说:Hibernate session就是java.sql.Connection的一层高级封装,一个session对应了一个Connection。
0 0
- hibernate一些底层实现的解析
- 黑马程序员:hibernate分页底层的实现
- 计算机底层实现的一些理解思路
- 解析unordeded_map和unordeded_set的底层实现
- java中一些容器底层的数据结构解析
- BBS项目笔记之八:hibernate底层查询的实现
- linux 文件描述符的一些底层实现
- ArrayList底层实现源码解析
- 一些单片机的底层论坛
- SpringMVC框架的一些底层
- 用hibernate实现数据库底层分页
- hibernate 延迟加载底层实现机制
- 指针的底层实现
- 图的底层实现
- HashMap的底层实现
- HashMap的底层实现
- HashMap的底层实现
- 变量的底层实现
- tomcat结合nginx使用小结
- 使用chrome浏览器对javascript进行调试
- 配置android开发环境
- Js获取当前日期时间及其它操作
- OC用Dispatch处理并发完成所有任务之后执行最终的结果
- hibernate一些底层实现的解析
- 【jQuery】removeAttr(name)和removeClass(class)
- 简单的ListView
- 第二届CCF计算机职业资格认证考试题解(C++)
- 【android】getCacheDir()、getFilesDir()、getExternalFilesDir()、getExternalCacheDir()的作用
- iOS学习之UIScrollView及UIPageControl的使用
- IOS查看UUID
- 使用Mahout搭建推荐系统之入门篇1-搭建REST风格简单推荐系统
- [Android]android.graphics.Camera实现图像的旋转、缩放,配合Matrix实现图像的倾斜