Connection and transaction
-
Hello, I have a global connection object. I have few methods, each of them do database operations, each of the method use one connection object. In order to use the transactions do I need to use a global command object too? class operation { OracleConnection oCon = new OracleConnection(); MethoodA() MethoodB() MethoodC() } MethodA() { Command object is used for database operation } Will this Command Object will also be a global variable access to all the Methods?
Regards, Pavas
-
Hello, I have a global connection object. I have few methods, each of them do database operations, each of the method use one connection object. In order to use the transactions do I need to use a global command object too? class operation { OracleConnection oCon = new OracleConnection(); MethoodA() MethoodB() MethoodC() } MethodA() { Command object is used for database operation } Will this Command Object will also be a global variable access to all the Methods?
Regards, Pavas
-
Its a Windows service. And it does lot of operation hence I created a global connection. Also I need to implement the Transactions between different methods which do different operation on database. Please suggest what can be the good approach
Regards, Pavas
-
Its a Windows service. And it does lot of operation hence I created a global connection. Also I need to implement the Transactions between different methods which do different operation on database. Please suggest what can be the good approach
Regards, Pavas
ok i see why you went for a global then, cant say if that is still the best option but i dont know what exactly you are doing. What difference do the methods have, do they need different commands? if so you would change the global command with each method, so may as well just create the command you need in the method and pass to connection
-
ok i see why you went for a global then, cant say if that is still the best option but i dont know what exactly you are doing. What difference do the methods have, do they need different commands? if so you would change the global command with each method, so may as well just create the command you need in the method and pass to connection
-
each of the method have different sql operations and they insert records in the database. My question is, I want an approch to implement the transaction.
Regards, Pavas
All you need is the Connection and Transaction objects. You create the Transaction from the Connection, something like:
DbTransaction tran = conn.BeginTransaction();
Then, you can do everything off of these objects (create the commands from the connection, etc.); be sure to set the command's Transaction property approriately; so you'd have something like:
DbConnection conn = GetConnection();
DbTransaction tran = conn.BeginTransaction();
bool success = true;
...
success = MethodA(conn, tran);
success = success && MethodB(conn, tran);
success = success && MethodC(conn, tran);
if(!success)
tran.Rollback();
else
tran.Commit();
...private bool MethodA(DbConnection conn, DbTransaction tran)
{
bool result = true;
DbCommand comm = conn.CreateCommand();
comm.CommandText = ...
comm.Transaction = tran;
...
return result;
}
...
private bool MethodB(DbConnection conn, DbTransaction tran)
...
private bool MethodC(DbConnection conn, DbTransaction tran)
...If you want to make the Connection and Transaction objects class-level variables and have the methods access them that way instead of passing them in, is up to you; I just prefer this way.
Keep It Simple Stupid! (KISS)
-
All you need is the Connection and Transaction objects. You create the Transaction from the Connection, something like:
DbTransaction tran = conn.BeginTransaction();
Then, you can do everything off of these objects (create the commands from the connection, etc.); be sure to set the command's Transaction property approriately; so you'd have something like:
DbConnection conn = GetConnection();
DbTransaction tran = conn.BeginTransaction();
bool success = true;
...
success = MethodA(conn, tran);
success = success && MethodB(conn, tran);
success = success && MethodC(conn, tran);
if(!success)
tran.Rollback();
else
tran.Commit();
...private bool MethodA(DbConnection conn, DbTransaction tran)
{
bool result = true;
DbCommand comm = conn.CreateCommand();
comm.CommandText = ...
comm.Transaction = tran;
...
return result;
}
...
private bool MethodB(DbConnection conn, DbTransaction tran)
...
private bool MethodC(DbConnection conn, DbTransaction tran)
...If you want to make the Connection and Transaction objects class-level variables and have the methods access them that way instead of passing them in, is up to you; I just prefer this way.
Keep It Simple Stupid! (KISS)