Configure mybatis to use an existing connection

痞子三分冷 提交于 2021-01-29 01:00:25

问题


I want to set up a connection between my application and a Oracle database. I do not have the following database information:

  1. URL
  2. user name
  3. password

What I can retrieve is a valid java.sql.Connection by using the API provided by Blackboard.

Is it possible to set up mybatis in this case?

I am using the configuration shown below:

<?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>

This is how I get a new SqlSessionFactory instance:

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

Afterwards, I try to retrieve a session by this code:

session = sqlSessionFactory.openSession(connection);

I have checked that the connection is valid with:

  1. connection.isValid(3);, which returns true
  2. a PreparedStatement can be executed successfully

However, sqlSessionFactory.openSession(connection) generates errors with the following stack trace:

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:


From this error:oracle.jdbc.driver.LogicalConnection@1627c16 is closed. This connection is closed when you passed it to sqlSessionFactoryBean. You can call connection.getAutoCommit() to test the connection whether it support transaction.



来源:https://stackoverflow.com/questions/30295170/configure-mybatis-to-use-an-existing-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!