论坛首页 Java企业应用论坛

用DataSourceTransactionManager但不用JDBCTemplate

浏览 21994 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2005-03-17  
谁有经验,想使用DataSourceTransactionManager但是不想用JDBCTemplate

没使用 hibernate

最近研究了一下spring的事务管理,觉得挺方便.

以前的程序都是一直 try {} catch{} finally这样使用的,太麻烦.

但是又不想使用spring的jdbctemplate,因为自己已经对jdbc的操作进行了很多封装,例如prepareStatement,分页查询等等

如果这样使用DataSourceTransactionManager,和正常使用jdbctemplate相比,在配置上有什么不同哪? 有哪些注意事项哪? 或者说能用吗?

我的了解(代码不依赖spring的前提下)

  1.自己需要对DataSource进行包装一下,包装为TransactionAwareDataSourceProxy
  2.其他按照正常的事务配置

另外: 对 DataSourceTransactionManager中的javadoc :
引用

Supports custom isolation levels, and timeouts that get applied as appropriate JDBC statement query timeouts. To support the latter, application code must either use JdbcTemplate or call DataSourceUtils.applyTransactionTimeout for each created statement.


没太看明白
   发表时间:2005-03-18  
这是Spring中HibernateDaoSupport的解释,说得很明白
<p>This class will create its own HibernateTemplate if only a SessionFactory
* is passed in. The allowCreate flag on that HibernateTemplate will be true by
* default. A custom HibernateTemplate instance can be used through overriding
* <code>createHibernateTemplate</code>.
0 请登录后投票
   发表时间:2005-03-18  
我晕 你在说啥??
0 请登录后投票
   发表时间:2005-03-18  
使用DataSourceUtils中得到的connection就可以了,会自动加入到事务中的。applyTransactionTimeout如果你设置了timeout,那么要对你的每个statement都apply一下,才会有效果。
0 请登录后投票
   发表时间:2005-03-18  
to rongsantang:

你是怎么用的哪? 用jdbcTemplate吗?
0 请登录后投票
   发表时间:2005-03-18  
看一下JdbcTemplate的源代码就清楚了:
		Connection con = DataSourceUtils.getConnection(getDataSource(););;
		Statement stmt = null;
		try {
			Connection conToUse = con;
			if (this.nativeJdbcExtractor != null &&
					this.nativeJdbcExtractor.isNativeConnectionNecessaryForNativeStatements();); {
				conToUse = this.nativeJdbcExtractor.getNativeConnection(con);;
			}
			stmt = conToUse.createStatement();;
			DataSourceUtils.applyTransactionTimeout(stmt, getDataSource(););;
			Statement stmtToUse = stmt;
			if (this.nativeJdbcExtractor != null); {
				stmtToUse = this.nativeJdbcExtractor.getNativeStatement(stmt);;
			}
			Object result = action.doInStatement(stmtToUse);;
			SQLWarning warning = stmt.getWarnings();;
			throwExceptionOnWarningIfNotIgnoringWarnings(warning);;
			return result;
		}
		catch (SQLException ex); {
			throw getExceptionTranslator();.translate("executing StatementCallback", getSql(action);, ex);;
		}
		finally {
			JdbcUtils.closeStatement(stmt);;
			DataSourceUtils.closeConnectionIfNecessary(con, getDataSource(););;
		}

这两句跟你的问题相关,另外就是try catchl了。
Connection con = DataSourceUtils.getConnection(getDataSource());
stmt = conToUse.createStatement();
DataSourceUtils.applyTransactionTimeout(stmt, getDataSource());
0 请登录后投票
   发表时间:2005-03-18  
我的想法是不使用jdbcTemplate,

我自己已经对jdbc操作进行了很多封装

而且我不想代码内依赖spring

你先看看DataSourceTransactionManager的javadoc ,应该能更明白我的问题.

引用

Alternatively, you can also allow application code to work with the standard J2EE lookup pattern DataSource.getConnection(), for example for legacy code that is not aware of Spring at all. In that case, define a TransactionAwareDataSourceProxy for your target DataSource, and pass that proxy DataSource to your DAOs, which will automatically participate in Spring-managed transactions through it. Note that DataSourceTransactionManager still needs to be wired with the target DataSource, driving transactions for it.
0 请登录后投票
   发表时间:2005-03-18  
假设我自己封装的jdbc操作是这样的(实际已经很复杂的了),而且现在不能放弃. 又想使用spring的事务管理,应该如何做


public class DBAdapter implements IDBAdapter
{
	private DataSource m_DataSource;

    /** 数据库连接 */
    protected Connection m_Connection;

    /** 连接语句 */
    protected Statement m_Statement;

    /** 数据库预编译语句 */
    protected PreparedStatement m_PreStmt;

    /** 数据查询结果集 */
    protected ResultSet m_ResultSet;

    /** 数据库相关元数据 */
    protected ResultSetMetaData m_ResultSetMetaData;


    public BaseDBAdapter();
    {
        //init();;
    }
    
    public void setDataSource(DataSource aDS);
    {
    	m_DataSource= aDS;
    }
    
    public void connnect();
    {
		//...
        m_Connection = m_DataSource.getConnection();;
        m_Statement = m_Connection.createStatement();;    	
        //...
    }
    
    public void execQuery(String sSQL);
                    throws SQLException
    {
    	//...
        m_ResultSet = m_Statement.executeQuery(sSQL);;
    	//...
    }

    public void execUpdate(String sSQL); throws SQLException
    {
    	//...
         m_Statement.executeUpdate(sSQL);;
    	//...
    }
    
    
    public Object loadItem(Class aClass);
    {
    	Object aObj;
    	
    	//load result to Object
    	
    	return aObj;
    }

}

0 请登录后投票
   发表时间:2005-03-18  
没错啊,你只要保证你的connection是从DataSourceUtils拿到的,如果tx有timeout属性再对stmt applyTransactionTimeout一下就可以了。

最后把你的service用TransactionProxyFactoryBean proxy一下就ok了。
0 请登录后投票
   发表时间:2005-03-18  
doc中不是也说得很清楚了么,你只要对你的m_datasource做一个proxy,让它返回的connection都是从DataSourceUtils得来的就可以了。
In that case, define a TransactionAwareDataSourceProxy for your target DataSource, and pass that proxy DataSource to your DAOs
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics