配置mybatis以使用现有连接

时间:2015-05-18 04:34:55

标签: java oracle mybatis blackboard

我想在我的应用程序和Oracle数据库之间建立连接。我没有以下数据库信息:

  1. URL
  2. 用户名
  3. 密码
  4. 使用Blackboard提供的API,我可以检索的是有效的java.sql.Connection

    在这种情况下是否可以设置mybatis?

    我正在使用下面显示的配置:

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" 
     "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
    
        <settings>
            <setting name="useGeneratedKeys" value="true"/>
            <setting name="jdbcTypeForNull" value="NULL"/>
        </settings>
    
        <typeAliases>
            <typeAlias alias="assignment" type="simpleproj.assignment.model.Assignment"/>
            <typeAlias alias="assignmentLog" type="simpleproj.assignment.model.AssignmentLog"/>
        </typeAliases>
    
        <mappers>   
            <mapper resource="simpleproj/assignment/model/Assignment.xml" />
        </mappers>
    
    </configuration>
    

    这是我获得新SqlSessionFactory实例的方式:

    String resource = "mybatis-config.xml";
    Reader reader = Resources.getResourceAsReader(resource);
    return new SqlSessionFactoryBuilder().build(reader, "assignment");
    

    之后,我尝试通过以下代码检索会话:

    session = sqlSessionFactory.openSession(connection);
    

    我已检查connection是否有效:

    1. connection.isValid(3);,返回true
    2. PreparedStatement可以成功执行
    3. 但是,sqlSessionFactory.openSession(connection)会产生以下堆栈跟踪错误:

      Exception in thread "main" org.apache.ibatis.exceptions.PersistenceException:
      ### Error opening session.  Cause: java.lang.reflect.UndeclaredThrowableException
      ### Cause: java.lang.reflect.UndeclaredThrowableException
              at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:23)
              at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromConnection(DefaultSqlSessionFactory.java:102)
              at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSession(DefaultSqlSessionFactory.java:67)
              at simpleproj.assignment.dao.DatabaseDAO.getSession(DatabaseDAO.java:55)
              at simpleproj.assignment.dao.DatabaseDAO.deleteAssignmentList(DatabaseDAO.java:124)
              at simpleproj.assignment.AssignmentLoader.importData(AssignmentLoader.java:85)
              at simpleproj.assignment.AssignmentLoader.main(AssignmentLoader.java:49)
      Caused by: java.lang.reflect.UndeclaredThrowableException
              at $Proxy7.getAutoCommit(Unknown Source)
              at org.apache.ibatis.session.defaults.DefaultSqlSessionFactory.openSessionFromConnection(DefaultSqlSessionFactory.java:99)
              ... 5 more
      Caused by: java.lang.reflect.InvocationTargetException
              at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
              at java.lang.reflect.Method.invoke(Method.java:597)
              at blackboard.db.ConnectionManager$ConnectionProxy.invoke(ConnectionManager.java:1419)
              ... 7 more
      Caused by: java.sql.SQLException: Connection oracle.jdbc.driver.LogicalConnection@1627c16 is closed.
              at org.apache.commons.dbcp.DelegatingConnection.checkOpen(DelegatingConnection.java:398)
              at org.apache.commons.dbcp.DelegatingConnection.getAutoCommit(DelegatingConnection.java:337)
              ... 11 more
      

1 个答案:

答案 0 :(得分:0)

从此错误:oracle.jdbc.driver.LogicalConnection@1627c16已关闭。 将此连接传递给sqlSessionFactoryBean时将关闭此连接。 您可以调用connection.getAutoCommit()来测试连接是否支持事务。