请帮助防止此ODP.NET代码和事务的数据层重构 [英] Please help prevent data layer refactoring of this ODP.NET code and transactions
问题描述
我正在将Oracle 11g客户端与ODP.NET一起使用.我正在尝试添加条件交易处理.
I am using Oracle 11g client, with ODP.NET. I am trying to add conditional Transaction handling.
Dim ds As New DataSet()
Dim txn As OracleTransaction
Dim _beginTransaction as Bolean = true
Using conn As New OracleConnection(ConnString)
Try
conn.Open()
If _beginTransaction Then
txn = conn.BeginTransaction(IsolationLevel.Serializable)
End If
Dim adapter As OracleDataAdapter = New OracleDataAdapter()
adapter.SelectCommand = New OracleCommand(sSQL, conn)
For i As Integer = 0 To UBound(parameters, 1)
adapter.SelectCommand.Parameters.Add(parameters(i))
Next
adapter.Fill(ds)
If _beginTransaction Then
txn.Commit() //txn is undefined here? why?
End If
Catch e As Exception
txn.Rollback()
End Try
End Using
如何解决txn为空/空值的问题?错误是:在变量'txn'被赋值之前被使用.空引用异常可能在运行时导致.链接或指向解决方案的指针也将不胜感激.
How do I fix txn being nothing / null? The error is: Variable 'txn' is used before it has been assigned a value. A null reference exception could result at runtime. Links or pointers to solutions would be appreciated also.
编辑:感谢RichardOD指出您不能明确声明不能通过ODP.NET在存储过程中打开事务.我已验证这是一个问题.但是我们仍然没有弄清楚为什么会发生错误.我知道txn最初在if语句中被赋予w/w的值,但是在try/catch块之外进行定义应该使它不相关....对吗?还是编码不好?
Thanks to RichardOD for pointing out that you can not explicitly declare that a transaction cannot be opend up on stored procedures via ODP.NET. I have verified that this is an issue. BUT We still haven't figured out why the error is occuring. I understand that txn is initially given a value w/in an if statement, but being defined outside of the try/catch block should make that irrelevant.... right? Or is that bad coding?
推荐答案
假设_beginTransaction是布尔值,您是否在If _beginTransaction Then
之前将其设置为true?
Assuming _beginTransaction is a boolean have you set it to true before If _beginTransaction Then
?
在开始这笔交易之前,您是否还提交了上一笔交易? Oracle可以通过连接池和BeingTransaction做一些奇怪的事情.
Also have you committed the previous transaction before starting this one? Oracle can do weird stuff with connection pooling and BeingTransaction.
很久以前,我有一个与此类似的错误. 您看过这里吗?
A long time ago I had a bug similar to this. Have you looked here?
编辑-您是否要调用.NET存储过程? OracleConnection.BeginTransaction
不支持存储过程调用:
Edit- are you trying to call a .NET stored proc? OracleConnection.BeginTransaction
does not support stored procedure calls:
OracleConnection.BeginTransaction是 .NET存储过程不允许使用
OracleConnection.BeginTransaction is not allowed for .NET stored procedure
这篇关于请帮助防止此ODP.NET代码和事务的数据层重构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!