我目前对 JBoss 7 中的 Oracle JNDI 数据源的自动提交设置感到奇怪。
大纲
我要部署的应用程序(我可以检查但不能更改)从连接池获取连接并尝试在某些语句后提交。显然,连接默认将自动提交设置为“true”,因此会引发异常。
You cannot commit with autocommit set!
我不能做什么
由于我无法更改应用程序源,因此以下“解决方案”不适用:
con.setAutoCommit(false);
我尝试过的
我查看了 standalone.xml 的 XML 模式,在数据源定义中发现了两个有前途的元素:
<connection-property name="autoCommit">
false
</connection-property>
但这被忽略了。
此外,我尝试使用具有以下属性的 xa-datasource:
<xa-datasource-property name="autoCommit">
false
</xa-datasource-property>
但是 autoCommit 属性属于连接,而不是数据源,因此会引发 PropertyNotFound 异常。
我的问题
如何在 JBoss7 下的 JNDI 数据源中将 Autocommit 设置为 false,我是否遗漏了什么?
我的定义
<datasource jta="false" jndi-name="java:/*****" pool-name="*****" enabled="true">
<connection-url>jdbc:oracle:thin:@****:****:****</connection-url>
<connection-property name="autoCommit">
false
</connection-property>
<driver>oracle.jdbc</driver>
<pool>
<min-pool-size>10</min-pool-size>
<max-pool-size>100</max-pool-size>
<prefill>true</prefill>
<use-strict-min>false</use-strict-min>
<flush-strategy>FailingConnectionOnly</flush-strategy>
</pool>
<security>
<user-name>*****</user-name>
<password>*****</password>
</security>
<timeout>
<blocking-timeout-millis>5000</blocking-timeout-millis>
<idle-timeout-minutes>1</idle-timeout-minutes>
</timeout>
</datasource>
更新
代码片段:
public AccessManager(jeeves.resources.dbms.Dbms dbms, SettingManager sm)
throws SQLException
{
List operList = dbms.select("SELECT * FROM Operations").getChildren();
}
更新 2
我要部署的应用程序是 Geonetwork CSW 2.9.0,以防万一有人对此有经验。我必须配置一个 JNDI 数据源,因为空间索引只发生在容器管理的连接上。
更新 3
堆栈跟踪:
15:52:18,203 ERROR [jeeves.engine] (MSC service thread 1-4) Raised exception while starting appl handler. Skipped.
15:52:18,205 ERROR [jeeves.engine] (MSC service thread 1-4) Handler : org.fao.geonet.Geonetwork
15:52:18,206 ERROR [jeeves.engine] (MSC service thread 1-4) Exception : java.sql.SQLException: You cannot commit with autocommit set!
15:52:18,210 ERROR [jeeves.engine] (MSC service thread 1-4) Message : You cannot commit with autocommit set!
15:52:18,213 ERROR [jeeves.engine] (MSC service thread 1-4) Stack : java.sql.SQLException: You cannot commit with autocommit set!
at org.jboss.jca.adapters.jdbc.BaseWrapperManagedConnection.jdbcCommit(BaseWrapperManagedConnection.java:984)
at org.jboss.jca.adapters.jdbc.WrappedConnection.commit(WrappedConnection.java:757)
at jeeves.resources.dbms.Dbms.commit(Dbms.java:150)
at jeeves.resources.dbms.AbstractDbmsPool.close(AbstractDbmsPool.java:158)
at jeeves.server.resources.ResourceManager.release(ResourceManager.java:302)
at jeeves.server.resources.ResourceManager.close(ResourceManager.java:270)
at jeeves.server.JeevesEngine.initAppHandler(JeevesEngine.java:556)
at jeeves.server.JeevesEngine.init(JeevesEngine.java:182)
at jeeves.server.sources.http.JeevesServlet.init(JeevesServlet.java:87)
at javax.servlet.GenericServlet.init(GenericServlet.java:242)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1202)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1102)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3655)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:3873)
at org.jboss.as.web.deployment.WebDeploymentService.start(WebDeploymentService.java:90)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1811)
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1746)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
更新 4
刚刚意识到 Jeeves DBMS 是 JNDI 数据源的简单包装器,它获取连接就像任何人使用普通 JDBC 所做的那样。所以我回到了起点。
最佳答案
感谢您给我指示,找到了适合我的解决方案(更正: 解决方法)。我围绕原始数据源制作了一个包装器,覆盖了 getConnection() 方法并将其绑定(bind)在一个新的 JNDI 名称下。这样我就可以完全控制数据源,而不会丢失容器的任何功能。
关于java - 防止 JBoss 7 (Jeeves DBMS) 上 JNDI 数据源的 Oracle 连接自动提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20973437/