使用 OracleDataSource 时 DBUnit/Spring 事务不回滚
我正在尝试使用 DBUnit 将空间数据插入 Oracle 数据库,然后将其回滚。为了插入空间数据,我必须使用 Oracle10DataTypeFactory,它会引入空间数据类型的其他 DBUnit Oracle 扩展。在内部,他们将对 OraclePreparedStatement 进行强制转换。为了获得 OraclePreparedStatement,我使用 OracleDataSource。因此,当我使用以下 Spring 配置时:
<bean id="testDataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="URL" value="${database.url}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<property name="implicitCachingEnabled" value="false" />
<property name="explicitCachingEnabled" value="false" />
<property name="connectionCachingEnabled" value="false" />
</bean>
插入永远不会回滚。从以下测试中我知道它与 OracleDataSource 相关联。在不需要空间数据的表中插入一行。使用以下配置:
<bean id="testDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${user}/>
<property name="password" value="${password}"/>
</bean>
一切正常,插入在测试完成时回滚。使用 OracleDataSource 执行与前面所示相同的测试,忘记它,没有任何回滚。我已经研究过将自动提交设置为 true,但不幸的是 Oracle 没有提供一种在 OracelDataSource 上设置的方法,至少我找不到。因此,在我的测试设置中,我明确地将其设置为 false,如下所示:
connection = DataSourceUtils.getConnection(dataSource);
connection.setAutoCommit(false);
但即使这样似乎也被忽略了。有人遇到过类似的情况吗?
I'm trying to use DBUnit to insert spatial data into an Oracle database and then roll it back. In order to insert the spatial data, I must use Oracle10DataTypeFactory which pulls in the other DBUnit Oracle extensions for spatial data types. Internally, they will do a cast to an OraclePreparedStatement. In order to get an OraclePreparedStatement, I use an OracleDataSource. So when I use the following Spring configuration:
<bean id="testDataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
<property name="URL" value="${database.url}"/>
<property name="user" value="${user}"/>
<property name="password" value="${password}"/>
<property name="implicitCachingEnabled" value="false" />
<property name="explicitCachingEnabled" value="false" />
<property name="connectionCachingEnabled" value="false" />
</bean>
The inserts never roll back. I know it is associated with the OracleDataSource from the following testing. Insert a row into a table that does not require spatial data. Use the following configuration:
<bean id="testDataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${user}/>
<property name="password" value="${password}"/>
</bean>
Everything works fine and the insert is rolled back at test completion. Perform the same exact test with the OracleDataSource as shown previous, forget it, nothing is rolled back. I've looked at autocommit being set to true, but unfortunately Oracle doesn't provide a method for setting on the OracelDataSource, at least not one I could find. So in my test setup I explicitly set it to false with the following:
connection = DataSourceUtils.getConnection(dataSource);
connection.setAutoCommit(false);
But even that seems to be ignored. Has anybody run into a similar situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当事务启动时,Spring Role Back 才能正确工作。
因此,请确保您的测试使用 Spring Runner(例如,使用
@RunWith(SpringJUnit4ClassRunner.class)
注解测试)并使用@Transactional
注解测试。Spring Role Back only works correct, if a transaction is started.
So make sure that your test uses the Spring Runner (for example by annotating the test with
@RunWith(SpringJUnit4ClassRunner.class)
) and the test annotated by@Transactional
.