Oracle TransactionScope Rollback issue

G

Guest

I am having problems re-openning closed Oracle connections
(System.Data.OracleClient.OracleConnection) when using TransactionScope and
using the Rollback feature (i.e. not calling Complete on TransactionScope).

I create a simple for loop with a TransactionScope statement inside. All
that happens inside the TransactionScope section is an open and close of an
OracleConnection object, and no call to Complete on the TransactionScope
object (Rollback). The second iteration or the loop will hang when calling
Open on the OracleConnection object.

If you include the Complete call on the TransactionScope, it will work fine.
If you exclude TransactionScope, it will work fine. If you step through the
code, it will work fine. If you put a System.Threading.Thread.Sleep(50)
statement at the beginning of the loop, it will work fine. I thought it
might be a disposing/garbage collection issue, so I tried putting GC.Collect
at the start of the loop, but that didn't work. If I use Oracle's provider,
it will work fine (Oracle.DataAccess.Client). If you leave the connection
open when iterating through the loop, it appears to work fine.

I have the 9.2.00.54 Oracle client installed. I have some sample code
listed below.

We would really like to use the System.Data.OracleClient as opposed to the
Oracle.DataAccess.Client. So, any suggestions would be greatly appreciated.

Thanks.


using System;
using System.Collections.Generic;
using System.Text;
using System.Transactions;
using System.Data.OracleClient;
//using Oracle.DataAccess.Client;
using System.Data;

namespace ConsoleApplication2
{
class Program
{
const string DATASOURCE = "Data Source=test_db;User
Id=sam;Password=sam;";
static OracleConnection cn;
static int i;

static void Main(string[] args)
{
cn = new OracleConnection(DATASOURCE);
for (i = 0; i < 100; i++)
{
TransactionScopeTest();
//TransactionScopeCompleteTest();
//TransactionScopeTestWithSleep();
//ConnectionTest();
}
Console.ReadLine();
}

static void TransactionScopeTest()
{
using (TransactionScope ts = new TransactionScope())
{
OpenClose();
}
}

static void TransactionScopeCompleteTest()
{
using (TransactionScope ts = new TransactionScope())
{
OpenClose();
ts.Complete();
}
}

static void ConnectionTest()
{
OpenClose();
}

static void TransactionScopeTestWithSleep()
{
using (TransactionScope ts = new TransactionScope())
{
System.Threading.Thread.Sleep(50);
OpenClose();
}
}

static void OpenClose()
{
if (cn.State != ConnectionState.Open)
{
Console.WriteLine("ConnectionState: " + cn.State.ToString()
+ "; openning " + i.ToString());
cn.Open();
Console.WriteLine("ConnectionState: " + cn.State.ToString()
+ "; openned " + i.ToString());
}

if (cn.State != ConnectionState.Closed)
{
Console.WriteLine("ConnectionState is " +
cn.State.ToString() + "; closing " + i.ToString());
cn.Close();
}
Console.WriteLine("ConnectionState is " + cn.State.ToString() +
" " + i.ToString());
}

}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top