Re: TransactionScope problem?
Posted by: schizoid
Date: August 07, 2006 11:35PM

I've created a simple emulation of TransactionScope:

public class LightweightTransaction : IDisposable
{
private IDbConnection m_connection;
private IDbTransaction m_transaction;
private bool m_committed;

public LightweightTransaction()
: this(IsolationLevel.RepeatableRead)
{
}

public LightweightTransaction(IsolationLevel il)
{
m_connection = // Make a connection here
m_transaction = m_connection.BeginTransaction(il);
}

public IDbConnection Connection
{
get
{
return m_connection;
}
}

public void Complete()
{
m_transaction.Commit();
m_committed = true;
CloseConnection();
}

public void Dispose()
{
if (m_transaction != null)
{
if (!m_committed)
{
m_transaction.Rollback();
}
m_transaction.Dispose();
m_transaction = null;
}
CloseConnection();
}

public IDataReader ExecuteQueryReader(string query)
{
IDbCommand cmd = Connection.CreateCommand();
cmd.CommandText = query;
cmd.Transaction = m_transaction;
IDataReader ret = cmd.ExecuteReader();
return ret;
}

public int ExecuteNonQuery(string query)
{
IDbCommand cmd = Connection.CreateCommand();
cmd.Transaction = m_transaction;
cmd.CommandText = query;
int ret = cmd.ExecuteNonQuery();
return ret;
}

public int GetNextMaxKey(string tableName, string columnName)
{
// TODO how to make isolation level serializable if it isn't already?
using (IDataReader reader = ExecuteQueryReader(string.Format("select max({0})+1 from {1}", columnName, tableName)))
{
int ret = 1;
if (reader.Read() && !reader.IsDBNull(0))
{
ret = RDBMSLayer.GetInt(0, reader);
}
return ret;
}
}

private void CloseConnection()
{
if (m_connection != null)
{
m_connection.Dispose();
m_connection = null;
}
}
}

Options: ReplyQuote


Subject
Written By
Posted
July 18, 2006 12:57PM
July 25, 2006 07:09AM
August 01, 2006 08:44AM
August 06, 2006 10:08AM
Re: TransactionScope problem?
August 07, 2006 11:35PM
September 20, 2006 11:29AM
September 26, 2006 08:02AM
September 28, 2006 06:51PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.