本文介绍了配置 mybatis 使用现有连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

  1. 网址
  2. 用户名
  3. 密码

通过使用 Blackboard 提供的 API,我可以检索到一个有效的 java.sql.Connection.

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

这种情况下可以设置mybatis吗?

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>

这就是我如何获得一个新的 SqlSessionFactory 实例:

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);

我已检查 connection 是否有效:

I have checked that the connection is valid with:

  1. connection.isValid(3);,返回真
  2. 一个PreparedStatement可以成功执行
  1. connection.isValid(3);, which returns true
  2. a PreparedStatement can be executed successfully

但是,sqlSessionFactory.openSession(connection) 会产生错误,堆栈跟踪如下:

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

推荐答案

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

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.

这篇关于配置 mybatis 使用现有连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:31