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. MySQL类型属性Unsigned与ZeroFill

    1. Unsigned 就是将数字类型无符号化. int的类型范围是-2147483648~2147483647, int unsigned的类型范围是0~4294967295 Unsigned也可能 ...

  2. JAVA中REPLACE和REPLACEALL的区别(转)

    replace和replaceAll是JAVA中常用的替换字符的方法,它们的区别是:  1)replace的参数是char和CharSequence,即可以支持字符的替换,也支持字符串的替换(Char ...

  3. bzoj1016:[JSOI2008]最小生成树计数

    思路:模拟kruskal的过程,可以发现对于所有权值相同的边,有很多种选择的方案,而且权值不同的边并不会相互影响,因为先考虑权值较小的边,权值比当前权值大的边显然不在考虑范围之内,而权值比当前权值小的 ...

  4. debian 学习记录-3 -关于linux -1

    来源:<Debian标准教程>王旭 著 芬兰人Linus Trovalds 1991年1月2日····   2006年初发布内核2.6.15 使用Andrew Tanenbaum < ...

  5. Oracle配置

    1.安装Oracle11g--->通过Database Configuration Assistant创建新数据库 建数据库注意记住数据库名和SID,安装最后一步解锁scott[密码填tiger ...

  6. 一些浏览器HACKS

    1.选择器HACKS /*IE6及以下*/            *html #uno{...} /*IE7*/                    *:first-child+html #dos{ ...

  7. MySQL数据库远程访问的权限

    GRANT ALL PRIVILEGES ON *.* TO 'user'@'%'IDENTIFIED BY 'passwd' WITH GRANT OPTION;

  8. Silverlight应用程序中调用WCF Ria Services访问数据库图片

    WCF Ria Services(通常称为RIA服务),专门设计让Silverlight应用程序访问数据库,网上有关其示例应用都是基于简单的数据显示,其中MSDN网站上有详细的解决方案介绍,地址htt ...

  9. Linux CentOS下shell显示-bash-4.1$ 不显示用户名和主机名的解决方法

    CentOS下新增加一个用户,登录进去会发现shell脚本信息没有显示用户名和主机名,反而显示的是-bash-4.1$,如图所示: 而不是我们经常看到的username@hostname$的组合,看起 ...

  10. oracle常用SQL语句(汇总版)

    Oracle数据库常用sql语句 ORACLE 常用的SQL语法和数据对象一.数据控制语句 (DML) 部分 1.INSERT (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, ...