[英]Configure mybatis to use an existing connection
I want to set up a connection between my application and a Oracle database. 我想在我的应用程序和Oracle数据库之间建立连接。 I do not have the following database information:
我没有以下数据库信息:
What I can retrieve is a valid java.sql.Connection
by using the API provided by Blackboard. 我可以使用Blackboard提供的API检索有效的
java.sql.Connection
。
Is it possible to set up mybatis in this case? 在这种情况下可以设置mybatis吗?
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: 这是我获取新的
SqlSessionFactory
实例的方法:
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: 我检查了该
connection
是否有效:
connection.isValid(3);
, which returns true PreparedStatement
can be executed successfully PreparedStatement
可以成功执行 However, sqlSessionFactory.openSession(connection)
generates errors with the following stack trace: 但是,
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
From this error:oracle.jdbc.driver.LogicalConnection@1627c16 is closed. 从此错误起:oracle.jdbc.driver.LogicalConnection@1627c16已关闭。 This connection is closed when you passed it to sqlSessionFactoryBean.
当将该连接传递给sqlSessionFactoryBean时,该连接将关闭。 You can call connection.getAutoCommit() to test the connection whether it support transaction.
您可以调用connection.getAutoCommit()来测试连接是否支持事务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.