org.springframework.jdbc.datasource.DataSourceUtils

    /**
* Actually obtain a JDBC Connection from the given DataSource.
* Same as {@link #getConnection}, but throwing the original SQLException.
* <p>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).
* <p>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.
* <p>
* 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.
* <p>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);
}

org.springframework.jdbc.datasource的更多相关文章

  1. java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/TransactionAwareDataSourceProxy

    问题:Error creating bean with name 'sqlSessionFactory' defined in class path resource [applicationCont ...

  2. 【错误解决】Error creating bean with name 'transactionManager' :nested exception is java.lang.NoClassDefFoundError: org/springframework/jdbc/datasource/

    搭建ssh框架中新建JUint测试出现的问题.这个问题实在太伤脑筋....因为不好找到解决办法 直接先说解决方式:添加org.springframework.jdbc-XX.jar,然后build p ...

  3. org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection

    org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection 这个问题困扰许久,许久 ...

  4. Spring 整合Mybatis 出现了Cause: org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: Cannot create Poola

    我出现的 报错信息如下: ### Error querying database. Cause: org.springframework.jdbc.CannotGetJdbcConnectionExc ...

  5. 解决:No qualifying bean of type [org.springframework.jdbc.core.JdbcTemplate] found for dependency

    错误: Description: Field jdbcTemplate in com.gwd.dao.impl.IUserDaoImpl required a bean of type 'org.sp ...

  6. org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection 原因

    org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection 可能出现的原因     ...

  7. org.springframework.jdbc.BadSqlGrammarException

    org.springframework.jdbc.BadSqlGrammarException: ### Error querying database. Cause: java.sql.SQLExc ...

  8. HTTP Status 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGram

    HTTP Status 500 - Request processing failed; nested exception is org.springframework.jdbc.BadSqlGram ...

  9. 错误:org.springframework.jdbc.support.SQLErrorCodesFactory - SQLErrorCodes loaded

    使用spring+mybatis整合时报错:org.springframework.jdbc.support.SQLErrorCodesFactory - SQLErrorCodes loaded 错 ...

随机推荐

  1. SQLite 入门教程(一)基本控制台(终端)命令

    一.基本简介 SQLite 是一个自持的(self-contained).无服务器的.零配置的.事务型的关系型数据库引擎.因为他很小,所以也可以作为嵌入式数据库内建在你的应用程序中.SQLite 被应 ...

  2. 对象创建型模式------Singleton(单例模式)

    地址:http://blog.csdn.net/wuzhekai1985/article/details/6665869.仅供自己参考学习. 单例模式:保证一个类仅有一个实例,并提供一个访问它的全局节 ...

  3. mybatis 无法转换为内部表示 解决

    出现这个问题,一般是JavaBean定义的时候数据类型不准造成的. 其中javabean定义的字段的类型并不需要完全和数据库中的字段一样. 在mapper.xml中可能做了转换  比如 CASE WH ...

  4. 牛客_剑指offer_重建二叉树,再后续遍历_递归思想_分两端

       总结:    重建二叉树:其实就是根据前序和中序重建得到二叉树,得到后续,只要输出那边设置输出顺序即可 [编程题]重建二叉树 输入某二叉树的前序遍历和中序遍历的结果,请重建出该二叉树.假设输入的 ...

  5. JAVA的StringBuffer类(转载整理)____非常重要的一个类,线程安全,不用每次创建一个对象,以及和String的区别

    核心部分转载自:http://www.cnblogs.com/springcsc/archive/2009/12/03/1616330.html StringBuffer类和String一样,也用来代 ...

  6. (转)QRCODE二维码介绍及常用控件推荐

    什么是QR Code码? QR Code码是由日本Denso公司于1994年9月研制的一种矩阵二维码符号,它具有一维条码及其它二维条码所具有的信息容量大.可靠性高.可表示汉字及图象多种文字信息.保密防 ...

  7. [译]CSS content

    原文地址:http://css-tricks.com/css-content/ CSS中有一个属性content,只能和伪元素:before和:after一起使用,他们的写法像伪类选择器(前面有冒号) ...

  8. linux 配置Socks5

    1.配置 Socks5 编译环境. yum -y install gcc automake autoconf libtool make 2.安装 Socks5 需要的包. yum -y install ...

  9. RequireJs 依赖管理使用

    What? 声明不同js文件之间的依赖,可以按需.并行.延时载入js库,可以让我们的代码以模块化的方式组织. When? 对于中大型项目,为了团队成员更好得发挥协同力,各自管理各自的JS代码,按需调用 ...

  10. JS访问Struts 2 ValueStack中的内容

    /* * var myArray = new Array("${vacant[0]}", "${vacant[1]}", "${vacant[2]}& ...