博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
org.springframework.jdbc.datasource
阅读量:6982 次
发布时间:2019-06-27

本文共 5574 字,大约阅读时间需要 18 分钟。

org.springframework.jdbc.datasource.DataSourceUtils

 

/**     * Actually obtain a JDBC Connection from the given DataSource.     * Same as {
@link #getConnection}, but throwing the original SQLException. *

Is aware of a corresponding Connection bound to the current thread, for example * when using {

@link DataSourceTransactionManager}. Will bind a Connection to the thread * if transaction synchronization is active (e.g. if in a JTA transaction). *

Directly accessed by {

@link TransactionAwareDataSourceProxy}. * @param dataSource the DataSource to obtain Connections from * @return a JDBC Connection from the given DataSource * @throws SQLException if thrown by JDBC methods * @see #doReleaseConnection */ public static Connection doGetConnection(DataSource dataSource) throws SQLException { Assert.notNull(dataSource, "No DataSource specified"); ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) { conHolder.requested(); if (!conHolder.hasConnection()) { logger.debug("Fetching resumed JDBC Connection from DataSource"); conHolder.setConnection(dataSource.getConnection()); } return conHolder.getConnection(); } // Else we either got no holder or an empty thread-bound holder here. logger.debug("Fetching JDBC Connection from DataSource"); Connection con = dataSource.getConnection(); if (TransactionSynchronizationManager.isSynchronizationActive()) { logger.debug("Registering transaction synchronization for JDBC Connection"); // Use same Connection for further JDBC actions within the transaction. // Thread-bound object will get removed by synchronization at transaction completion. ConnectionHolder holderToUse = conHolder; if (holderToUse == null) { holderToUse = new ConnectionHolder(con); } else { holderToUse.setConnection(con); } holderToUse.requested(); TransactionSynchronizationManager.registerSynchronization( new ConnectionSynchronization(holderToUse, dataSource)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != conHolder) { TransactionSynchronizationManager.bindResource(dataSource, holderToUse); } } return con; }

org.mybatis.spring.transaction.SpringManagedTransaction

/**   * Gets a connection from Spring transaction manager and discovers if this   * {
@code Transaction} should manage connection or let it to Spring. *

* It also reads autocommit setting because when using Spring Transaction MyBatis * thinks that autocommit is always false and will always call commit/rollback * so we need to no-op that calls. */ private void openConnection() throws SQLException { this.connection = DataSourceUtils.getConnection(this.dataSource); this.autoCommit = this.connection.getAutoCommit(); this.isConnectionTransactional = DataSourceUtils.isConnectionTransactional(this.connection, this.dataSource); if (LOGGER.isDebugEnabled()) { LOGGER.debug( "JDBC Connection [" + this.connection + "] will" + (this.isConnectionTransactional ? " " : " not ") + "be managed by Spring"); } }

 

 

 

 

org.mybatis.spring.SqlSessionUtils

public void beforeCompletion() {            if(!this.holder.isOpen()) {                if(SqlSessionUtils.LOGGER.isDebugEnabled()) {                    SqlSessionUtils.LOGGER.debug("Transaction synchronization deregistering SqlSession [" + this.holder.getSqlSession() + "]");                }                TransactionSynchronizationManager.unbindResource(this.sessionFactory);                this.holderActive = false;                if(SqlSessionUtils.LOGGER.isDebugEnabled()) {                    SqlSessionUtils.LOGGER.debug("Transaction synchronization closing SqlSession [" + this.holder.getSqlSession() + "]");                }                this.holder.getSqlSession().close();            }        }

 

 

 

/**     * Actually close the given Connection, obtained from the given DataSource.     * Same as {
@link #releaseConnection}, but throwing the original SQLException. *

Directly accessed by {

@link TransactionAwareDataSourceProxy}. * @param con the Connection to close if necessary * (if this is {
@code null}, the call will be ignored) * @param dataSource the DataSource that the Connection was obtained from * (may be {
@code null}) * @throws SQLException if thrown by JDBC methods * @see #doGetConnection */ public static void doReleaseConnection(Connection con, DataSource dataSource) throws SQLException { if (con == null) { return; } if (dataSource != null) { ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); if (conHolder != null && connectionEquals(conHolder, con)) { // It's the transactional Connection: Don't close it. conHolder.released(); return; } } logger.debug("Returning JDBC Connection to DataSource"); doCloseConnection(con, dataSource); }

 

转载于:https://www.cnblogs.com/softidea/p/5951685.html

你可能感兴趣的文章
Dart的HTTP请求和响应(1)
查看>>
寻找最大的K个数,Top K问题的堆实现
查看>>
自动发布工具应该具备的11个标准特征
查看>>
页面设计四大基本原则
查看>>
2016及以后的自动化测试趋势 -《测试技术六月刊》
查看>>
基于Angular创建后台数据模拟(译)
查看>>
Spring中bean配置的继承
查看>>
用JSP实现学生查询
查看>>
企业网站怎么建设
查看>>
数据库和MySQL相关面试题目
查看>>
Yii 框架学习--01 框架入门
查看>>
All Things OpenTSDB
查看>>
android 网络通信框架volly
查看>>
poj 题型分类
查看>>
二分查找算法及其变种
查看>>
一个泛型冒泡排序的实现
查看>>
大型分布式网站架构设计与实践 第一章《面向服务的体系架构(SOA)》
查看>>
[From OpenBSD Man Page]PFSYNC
查看>>
自定义View时,用到Paint Canvas的一些温故,讲讲平时一些效果是怎么画的(基础篇 一)...
查看>>
hdu 5131 Song Jiang's rank list 【2014ACM/ICPC亚洲区广州站-重现赛】
查看>>