论坛首页 Java企业应用论坛

请问ibatis如何关闭连接和结果集的

浏览 9927 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-04-04  
我用的ibatis2.3,没用原来的dao框架,直接写代码
 try {
      sqlMap.startTransaction();
      List list = sqlMap.queryForList(
          "test.selectBypk", parms);
      sqlMap.commitTransaction();
      return list;
    }
    finally {
      sqlMap.endTransaction();
    }
打开了log4j,
log4j.logger.com.ibatis=DEBUG,stdout,logfile
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=DEBUG
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=DEBUG,stdout,logfile
log4j.logger.java.sql.Connection=DEBUG,stdout,logfile
log4j.logger.java.sql.Statement=DEBUG,stdout,logfile
log4j.logger.java.sql.PreparedStatement=DEBUG,stdout,logfile
log4j.logger.java.sql.ResultSet=DEBUG,stdout,logfile

可以看到输出了相关的sql,可是并没有看到关闭或返回连接的输出,也没有关闭结果集的输出
请问ibatis是在哪控制的?我如何在日志文件中看到?

   发表时间:2007-04-04  
自己先顶一下,我用的tomcat自带的dbcp,mysql数据库,运行起来没问题,只是想到了这个如何关闭的问题

另外,在官方的开发指南中看到这样一句话:
SqlMapClient事务处理使用Java的ThreadLocal保存事务对象。这意味着在处理事务时,每个调用startTransaction()的线程,将得到一个唯一的Connection对象。将一个Connection对象返回数据源(或关闭连接)唯一的方法是调用commitTransaction()或rollbackTransaction()方法。否则,会用光连接池中的连接并导致死锁。

也就是说,照我上面的程序做法,是关闭了连接的,可为什么日志里看不到?结果集又在何处关闭,还是用commitTransaction()的时候就一起关闭了?
0 请登录后投票
   发表时间:2007-04-04  
自动的
0 请登录后投票
   发表时间:2007-04-04  
panwj 写道
自动的

我把log4j开到了 debug级别居然看不到相关的关闭信息?
0 请登录后投票
   发表时间:2007-04-07  
看一下 iBatis 的源码就什么都知道了。。。
0 请登录后投票
   发表时间:2007-04-07  
自动的,大至是基于‘模板回调’设计模式的实现
装配并实例化一个sqlMap对象后,他已经包含了所有的基础代码,如打开数据库连接,sqmMap引擎初始化,关闭连接等
只要写少量的事务处理代码,其他的都被包办了
0 请登录后投票
   发表时间:2007-04-07  
刚才看了一下IBatis的源码,在com/ibatis/sqlmap/engine/transaction包下面有一整套实现sqlMap管理事务范围的实现类,sqlMap.commitTransaction()执行时实际调用IBatis的TransactionManager.commit(SessionScope session),代码自动执行。
0 请登录后投票
   发表时间:2007-04-07  
飞花逐月 写道
panwj 写道
自动的

我把log4j开到了 debug级别居然看不到相关的关闭信息?


给你看段代码:
  public void executeQuery(RequestScope request, Connection conn, String sql, Object[] parameters, int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException {
    ErrorContext errorContext = request.getErrorContext();
    errorContext.setActivity("executing query");
    errorContext.setObjectId(sql);
    PreparedStatement ps = null;
    ResultSet rs = null;
    setupResultObjectFactory(request);
    try {
      errorContext.setMoreInfo("Check the SQL Statement (preparation failed).");
      Integer rsType = request.getStatement().getResultSetType();
      if (rsType != null) {
        ps = prepareStatement(request.getSession(), conn, sql, rsType);
      } else {
        ps = prepareStatement(request.getSession(), conn, sql);
      }
      setStatementTimeout(request.getStatement(), ps);
      Integer fetchSize = request.getStatement().getFetchSize();
      if (fetchSize != null) {
        ps.setFetchSize(fetchSize.intValue());
      }
      errorContext.setMoreInfo("Check the parameters (set parameters failed).");
      request.getParameterMap().setParameters(request, ps, parameters);
      errorContext.setMoreInfo("Check the statement (query failed).");
      ps.execute();
      errorContext.setMoreInfo("Check the results (failed to retrieve results).");

      // Begin ResultSet Handling
      rs = handleMultipleResults(ps, request, skipResults, maxResults, callback);
      // End ResultSet Handling
    } finally {
      try {
        closeResultSet(rs);
      } finally {
        closeStatement(request.getSession(), ps);
      }
    }

  }


看看里面如何打开和关闭,其他可以查阅iBatis代码
0 请登录后投票
   发表时间:2007-04-16  
ecsoftcn 写道
飞花逐月 写道
panwj 写道
自动的

我把log4j开到了 debug级别居然看不到相关的关闭信息?


给你看段代码:
  public void executeQuery(RequestScope request, Connection conn, String sql, Object[] parameters, int skipResults, int maxResults, RowHandlerCallback callback) throws SQLException {
    ErrorContext errorContext = request.getErrorContext();
    errorContext.setActivity("executing query");
    errorContext.setObjectId(sql);
    PreparedStatement ps = null;
    ResultSet rs = null;
    setupResultObjectFactory(request);
    try {
      errorContext.setMoreInfo("Check the SQL Statement (preparation failed).");
      Integer rsType = request.getStatement().getResultSetType();
      if (rsType != null) {
        ps = prepareStatement(request.getSession(), conn, sql, rsType);
      } else {
        ps = prepareStatement(request.getSession(), conn, sql);
      }
      setStatementTimeout(request.getStatement(), ps);
      Integer fetchSize = request.getStatement().getFetchSize();
      if (fetchSize != null) {
        ps.setFetchSize(fetchSize.intValue());
      }
      errorContext.setMoreInfo("Check the parameters (set parameters failed).");
      request.getParameterMap().setParameters(request, ps, parameters);
      errorContext.setMoreInfo("Check the statement (query failed).");
      ps.execute();
      errorContext.setMoreInfo("Check the results (failed to retrieve results).");

      // Begin ResultSet Handling
      rs = handleMultipleResults(ps, request, skipResults, maxResults, callback);
      // End ResultSet Handling
    } finally {
      try {
        closeResultSet(rs);
      } finally {
        closeStatement(request.getSession(), ps);
      }
    }

  }


看看里面如何打开和关闭,其他可以查阅iBatis代码



谢谢楼上诸位。明白了............
0 请登录后投票
论坛首页 Java企业应用版

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