我正在使用一些遗留的 JDBC 代码,并将其配置为使用容器管理的持久性,到目前为止,它在为我管理的事务中运行良好,但它在存储过程调用上失败。
一些存储过程会创建临时表,这需要在事务中间提交。因此,我收到一个异常,提示如果我使用容器管理的持久性,则无法调用提交。
有人知道解决这个问题的方法吗?
更多信息:
如果我在查询末尾添加 commit(),我会得到:
DSRA9350E:全局事务期间不允许进行 Connection.commit 操作。
所以我推测 Sybase JDBC 4 XA 驱动程序正在为我管理事务。如果我在代码中抛出异常,它就会回滚。
public Connection getConnection() throws SQLException {
if ( connection == null ) {
this.connection = dataSource.getConnection();
this.connection.setAutoCommit(!useTransaction);
this.connection.setTransactionIsolation(transactionIsolationLevel);
}
logger.info("Connection [ "+ connection.toString() +" ]");
return connection;
}
我获得连接的部分通常将自动提交设置为“假”,而对于存储过程,它将自动提交设置为“真”。但无论哪种方式,带有临时表的存储过程都会得到:
java.sql.SQLException:多语句事务中不允许使用 SELECT INTO 命令。
有点困惑。数据源是由容器设置的,我只是使用资源引用标签从上下文中获取它们。它们是 XA 数据源,因此它们提供全局事务。我尝试用 Spring 禁用它:
<context:component-scan base-package="package.path.to.class.with.method" />
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.transaction.jta.WebSphereUowTransactionManager" />
通过提供 类上的 @Component 和方法上的 @Transactional
@Transactional(propagation=Propagation.NOT_SUPPORTED)
public ResultSet executeProcedure(String sql, String[] parameterTypes,
String[] parameterValues) throws SEEException {
SqlParameters parameters = this.convertParameters(parameterTypes, parameterValues);
return super.executeProdedure(sql, parameters);
}
但我仍然收到错误。
存储过程看起来有点像这样(procxmode 是 UNCHAINED)。存储过程 defn 本身就是一个事务,所以我认为我一定没有 Activity 事务进入。但我无法编辑存储过程本身。它已经生产多年了:
define sp_example
begin
create table #temp {}
begin
insert into #temp {}
end
begin
select from #temp {}
end
end
最佳答案
ejb 2.x 中的术语“容器管理的持久性”意味着 EJB 容器处理实体 bean 所需的所有数据库访问,范围由方法实现定义。如果您需要控制事务,例如在方法执行中调用 commit,您将被迫使用 Bean 管理的持久性和 UserTransaction 接口(interface)方法来控制它。
不幸的是,ejb 2.x 规范不允许您拥有混合的 CMP/BMP 实体 bean,您需要选择一个适合您的业务案例的实体 bean。
关于java - 容器管理的持久性和创建临时表的存储过程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17853435/