请帮助防止此 ODP.NET 代码和事务的数据层重构

发布于 2024-08-03 01:04:12 字数 1235 浏览 3 评论 0原文

我正在使用 Oracle 11g 客户端和 ODP.NET。我正在尝试添加条件事务处理。

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”在赋值之前就被使用了。运行时可能会导致空引用异常。指向解决方案的链接或指针也将不胜感激。

编辑:感谢 RichardOD 指出,您无法明确声明无法通过 ODP.NET 在存储过程上打开事务。我已经证实这是一个问题。但我们仍然没有弄清楚为什么会发生错误。我知道 txn 最初在 if 语句中被赋予一个值,但是在 try/catch 块之外定义应该使得它无关......对吗?或者说这是糟糕的编码?

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

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.

Edit: 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

喜爱纠缠 2024-08-10 01:04:12

假设 _beginTransaction 是一个布尔值,您是否在 If _beginTransaction then 之前将其设置为 true ?

另外,在开始这一笔交易之前,您是否已经提交了上一笔交易? Oracle 可以通过连接池和 BeingTransaction 做一些奇怪的事情。

很久以前我就遇到过类似的bug。 您看过这里吗?

编辑 - 您是否尝试调用 .NET 存储过程? OracleConnection.BeginTransaction 不支持存储过程调用

OracleConnection.BeginTransaction 是
不允许使用 .NET 存储过程

Assuming _beginTransaction is a boolean have you set it to true before If _beginTransaction Then ?

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?

Edit- are you trying to call a .NET stored proc? OracleConnection.BeginTransaction does not support stored procedure calls:

OracleConnection.BeginTransaction is
not allowed for .NET stored procedure

z祗昰~ 2024-08-10 01:04:12

问:赋值后是否立即为空?如果不是,什么时候它会变成空?如果它立即为空,则可能是连接池的问题。如果从 conn 获取适配器或填充后,那就更疯狂了......

但我会尝试找出答案

Question: Is it null immediately after being asigned? And if not, when does it become null? If it's null after immediately, it might be the connection pooling stuff. If after getting the adapter from conn or after filling it, then it's even crazier...

But I would try and find out

难忘№最初的完美 2024-08-10 01:04:12

Oracle 不需要事务来选择数据。为什么你要尝试打开一个?

编辑:
如果您的 vb 代码是从 oracle 调用的(通过 .net 集成),那么就没有 RichardOD 所写的事务支持。请澄清一下环境。

执行的sql语句是动态的,并在sSQL中给出。该命令已准备好并提供给填充数据集的 DataAdapter。比你只能执行 SELECT 语句。否则没有结果。

OR

因为参数也准备好了。您正在调用一个存储过程(没有告诉 CommandType 是 StoredProcedure)。您的参数之一是引用游标参数,它将提取到数据集中。正确的?

Oracle 不像 sql server 那样需要显式事务。 Oracle 使用会话中的第一个 dml 语句启动隐式事务。副作用是,如果您没有启动事务,则无法提交隐式事务。我不知道是否可以通过连接对象访问隐式事务。

Oracle does not require a transaction for selecting data. Why do you try open one?

EDIT:
If your vb code is called from oracle (via .net integration) than there is no transaction support as RichardOD wrote. Please clarify the environment.

The sql statement executed is dynamic and given in sSQL. The command is prepared and given to a DataAdapter that fills a DataSet. Than you can only execute SELECT statements. Otherwise there is no result.

OR

Because the parameters are prepared too. You are calling a stored procedure (without telling the the CommandType is StoredProcedure). One of your parameters is a ref cursor parameter which will fetched into the DataSet. Right?

Oracle does not need explicit transactions as sql server does. Oracle starts an implicit transaction with the first dml statement in your session. The sideeffect is, if you did not start an transaction you cannot commit the implicit transaction. I do not know if there is access to implicit transaction via the connection object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文