- Home
- LLBLGen Pro
- Architecture
Transaction Object in LLBLGenPro
Joined: 28-Oct-2009
Transaction transactionManager = new Transaction(); Entity entity = new Entity(23);// 23 id i database entity.filedname = "xyz" // field to be updated transactionManager .Add(entity); entity.save(); //this saves everything and transaction.commit or rollback have no relevence.
transactionManager .Commit(); transactionManager .RollBack();
the above commit() and Rolback() methods are not wrking on the entity which is added to transaction.
behavior:
- Even if i comment the commit or rollback. the save happens.
When we add something to a transaction, we expect the commit() to be called for things in the transaction to be committed. the rollback should revert back the changes.
This is not happening. we are doing POC with LLBLgen and need to convinse our managers.
please help us out to understand the transaction part of LLBLgen . i did read the documents. but document says as i have written.
Joined: 17-Aug-2003
The commit will tell the db the save is to be kept. If you rollback the inserted row will be removed. So before the commit/rollback, the row IS inserted, as with every transactional system.
(edit) andplease post these kind of questions in general/ generated code next time, as architecture is for architectural discussons.
Joined: 28-Oct-2009
Otis,
Thanks for your reply. I will make sure that i post these kind of questions in general category going forward. Just continuing with the loop here .
The changes in database are not temporary and they are permanent even if we are not calling Commit(). so, what is happening is all the updates are happening with in the Save() method irrespective of the Commit and Rollback method.
Even if we rollback after the Save method, the data in database is not getting rolled back.
We are working on the SQL server database and all the LLBLgen code is generated using SQL Server database as backend.
I know this behavior is odd and i think there is some problem with what we have done.
Can you please give me some ideas on the same. PLEASE LET ME KNOW HOW WE SHOULD APPROACH THIS
The way things are working , the transaction concept is not satisfied.
please help.
i WILL MOVE TO GENERAL FORUM AFTER THIS. tHANKS
Joined: 28-Oct-2009
here we go.
Database : SQL Server, But we tested on Oracle also and behavior is same. so i think its not Datbase issue.
The code giving issue :
public void UpdateCliamantTypeListInfo(UiClaimmstrVlntryAddrDTO dto) {
string name = Guid.NewGuid().ToString();
Transaction transactionManager = new Transaction(IsolationLevel.ReadCommitted, name);
try
{
TClamMstrDTO clmMstDTO = new TClamMstrDTO();
clmMstDTO.IsNew = dto.IsNew;
//clmMstDTO.Clmid = dto.Clmid_;
clmMstDTO.ClmType = dto.ClmType;
clmMstDTO.ClmStus = dto.ClmStus;
clmMstDTO.CrtdBy = " XXX ";
clmMstDTO.CrtdDt = DateTime.Now;
clmMstDTO.UpdtdBy = " XXX ";
clmMstDTO.UpdtdDt = DateTime.Now;
TClamMstrEntity clmMstrEnt = new TClamMstrEntity(clmMstDTO);
transactionManager.Add(clmMstrEnt);
clmMstrEnt.Save();
TClamAddrDTO clmAddrDTO = new TClamAddrDTO();
clmAddrDTO.IsNew = dto.IsNew;
clmAddrDTO.Clmid = clmMstrEnt.Clmid;
clmAddrDTO.AddrLine1 = dto.AddrLine1;
clmAddrDTO.AddrLine2 = dto.AddrLine2;
clmAddrDTO.AddrType = dto.AddrType;
clmAddrDTO.City = dto.City;
clmAddrDTO.State = dto.State;
clmAddrDTO.Zip = dto.Zip;
clmAddrDTO.CrtdDt = DateTime.Now;
clmAddrDTO.CrtdBy = "XXX";
clmAddrDTO.UpdtdDt = DateTime.Now;
clmAddrDTO.UpdtdBy = "XXX";
TClamAddrEntity clmAddrEnt = new TClamAddrEntity(clmAddrDTO);
transactionManager.Add(clmAddrEnt);
clmAddrEnt.Save();
TVlntryClmDTO objVlnDTO = new TVlntryClmDTO();
objVlnDTO.IsNew = dto.IsNew;
objVlnDTO.Clmid = clmMstrEnt.Clmid;
objVlnDTO.ClmntFname = dto.ClmntFname;
objVlnDTO.ClmntLname = dto.ClmntLname;
objVlnDTO.ClmntSsn = dto.ClmntSsn;
objVlnDTO.CrtdBy = " XXX ";
objVlnDTO.CrtdDt = DateTime.Now;
objVlnDTO.UpdtdBy = " XXX ";
objVlnDTO.UpdtdDt = DateTime.Now;
TVlntryClmEntity vlnEnt = new TVlntryClmEntity(objVlnDTO);
transactionManager.Add(vlnEnt);
vlnEnt.Save();
//transactionManager.Commit();
//transactionManager.Rollback();
}
catch (Exception ex)
{
transactionManager.Rollback();
ExceptionManager.HandleException(ex);
}
finally
{
transactionManager.Dispose();
}
}
Note :
We commented the commit transaction, but still the Save above for the entities is working. Are we doing anything wrong
We tried Rollback , but still nothing is rolling back.
If code written is correct. Could it be because of the transaction code generated otr any other reason.
please suggest.
Joined: 28-Oct-2009
FYI..
Transaction.CS
/////////////////////////////////////////////////// // This is generated code - do not make any direct // changes to this file as your changes may be lost /////////////////////////////////////////////////// using System; using System.Data;
using uFACTS.BusinessEntities.HelperClasses;
using SD.LLBLGen.Pro.ORMSupportClasses;
namespace uFACTS.BusinessEntities.HelperClasses { /// <summary> /// Specific implementation of the Transaction class. The constructor will take care of the creation of the physical transaction and the /// opening of the connection. The transaction object is ready to use as soon as the constructor succeeds. /// </summary> public partial class Transaction : TransactionBase { /// <summary> /// CTor. Will read the connection string from an external source. Opens connection, class /// </summary> /// <param name="transactionIsolationLevel">IsolationLevel to use in the transaction</param> /// <param name="name">The name of the transaction to use.</param> public Transaction(IsolationLevel transactionIsolationLevel, string name):base(transactionIsolationLevel, name) { // empty }
/// <summary>
/// CTor.
/// </summary>
/// <param name="transactionIsolationLevel">IsolationLevel to use in the transaction</param>
/// <param name="name">The name of the transaction to use.</param>
/// <param name="connectionString">Connection string to use in this transaction</param>
public Transaction(IsolationLevel transactionIsolationLevel, string name, string connectionString):base(transactionIsolationLevel, name, connectionString)
{
// empty
}
/// <summary>
/// Creates a new IDbConnection instance which will be used by all elements using this ITransaction instance.
/// Reads connectionstring from .config file.
/// </summary>
/// <returns>new IDbConnection instance</returns>
protected override System.Data.IDbConnection CreateConnection()
{
return DbUtils.CreateConnection();
}
/// <summary>
/// Creates a new IDbConnection instance which will be used by all elements using this ITransaction instance
/// </summary>
/// <param name="connectionString">Connection string to use</param>
/// <returns>new IDbConnection instance</returns>
protected override System.Data.IDbConnection CreateConnection(string connectionString)
{
return DbUtils.CreateConnection(connectionString);
}
/// <summary>
/// Creates a new physical transaction object over the created connection. The connection is assumed to be open.
/// </summary>
/// <returns>a physical transaction object, like an instance of SqlTransaction.</returns>
protected override System.Data.IDbTransaction CreatePhysicalTransaction()
{
return DbUtils.CreateTransaction(base.ConnectionToUse, base.TransactionIsolationLevel, this.Name);
}
#region Included Code
#endregion
}
}
We have a static class as well:
TransactionManager.CS------------------------------
using System; using System.Collections.Generic; using SD.LLBLGen.Pro.ORMSupportClasses; using uFACTS.BusinessEntities.HelperClasses; using uFACTS.Utility.ServiceTier; using uFACTS.Utility.Logging; using System.Reflection; using System.Data;
namespace uFACTS.BusinessEntities { public static class TransactionManager {
public static Transaction Current
{
get
{
VerifyTransactionActive();
return GetTransaction(null);
}
}
public static Transaction GetTransaction(string transactionName)
{
int index = IndexOf(transactionName);
if (index >= 0)
{
return (Transaction)uSession.Context.Transactions[index];
}
return null;
}
public static Transaction New()
{
string name = Guid.NewGuid().ToString();
Logger.Write(LogSeverityType.Notice, "Database.NewTrans", "Transaction started", "Database:Transaction=" + name.ToString());
Transaction transaction = new Transaction(System.Data.IsolationLevel.ReadCommitted, name);
transaction.PhysicalTransaction.Connection.GetType().GetProperty("TransactionState", BindingFlags.Instance | BindingFlags.NonPublic).SetValue(transaction.PhysicalTransaction.Connection, 1, null);
uSession.Context.Transactions.Add(transaction);
return transaction;
}
public static bool SetCurrent(string transactionName)
{
int index = IndexOf(transactionName);
if (index >= 0)
{
if (uSession.Context.Transactions.Count > 0)
{
Transaction transaction = (Transaction)uSession.Context.Transactions[index];
uSession.Context.Transactions.RemoveAt(index);
uSession.Context.Transactions.Add(transaction);
}
return true;
}
return false;
}
public static void Save(string savePointName)
{
VerifyTransactionActive();
Save(null, savePointName);
}
/// <summary>
///
/// </summary>
/// <param name="transactionName"></param>
/// <param name="savePointName"></param>
public static void Save(string transactionName, string savePointName)
{
if (!string.IsNullOrEmpty(savePointName))
{
Transaction transaction = GetTransaction(transactionName);
if (transaction != null)
{
transaction.Save(savePointName);
}
}
}
/// <summary>
///
/// </summary>
/// <param name="savePointName"></param>
public static void Restore(string savePointName)
{
Restore(null, savePointName);
}
public static void Restore(string transactionName, string savePointName)
{
if (!string.IsNullOrEmpty(savePointName))
{
Transaction transaction = GetTransaction(transactionName);
if (transaction != null)
{
transaction.Rollback(savePointName);
}
}
}
public static void Commit()
{
Commit(null);
}
public static void Commit(string transactionName)
{
int index = IndexOf(transactionName);
if (index >= 0)
{
Transaction transaction = (Transaction)uSession.Context.Transactions[index];
Logger.Write(LogSeverityType.Notice, "Database.Commit", "Commit Transaction", "Database:Transaction=" + transaction.Name);
uSession.Context.Transactions.RemoveAt(index);
transaction.Commit();
transaction.Dispose();
}
}
public static void CommitAll()
{
while (uSession.Context.Transactions.Count > 0)
{
int pos = uSession.Context.Transactions.Count - 1;
Transaction transaction = (Transaction)uSession.Context.Transactions[pos];
Logger.Write(LogSeverityType.Notice, "Database.Commit", "Commit Transaction", "Database:Transaction=" + transaction.Name);
uSession.Context.Transactions.RemoveAt(pos);
transaction.Commit();
transaction.Dispose();
}
}
public static void Rollback()
{
Rollback(null);
}
public static void Rollback(string transactionName)
{
int index = IndexOf(transactionName);
if (index >= 0)
{
Transaction transaction = (Transaction)uSession.Context.Transactions[index];
Logger.Write(LogSeverityType.Notice, "Database.Rollback", "Rollback Transaction", "Database:Transaction=" + transaction.Name);
uSession.Context.Transactions.RemoveAt(index);
transaction.Rollback();
transaction.Dispose();
}
}
public static void RollbackAll()
{
while (uSession.Context.Transactions.Count > 0)
{
int pos = uSession.Context.Transactions.Count - 1;
Transaction transaction = (Transaction)uSession.Context.Transactions[pos];
Logger.Write(LogSeverityType.Notice, "Database.Rollback", "Rollback Transaction", "Database:Transaction=" + transaction.Name);
uSession.Context.Transactions.RemoveAt(pos);
transaction.Rollback();
transaction.Dispose();
}
}
public static void Add(ITransactionalElement participant)
{
Add(participant, false);
}
public static void Add(ITransactionalElement participant, bool force)
{
VerifyTransactionActive();
Add(null, participant, force);
}
public static void Add(string transactionName, ITransactionalElement participant, bool force)
{
if (force || (!uSession.Context.TransSuspended && participant != null))
{
Transaction transaction = GetTransaction(transactionName);
if (transaction != null)
{
transaction.Add(participant);
}
}
}
public static void Remove(ITransactionalElement participant)
{
Remove(null, participant);
}
public static void Remove(string transactionName, ITransactionalElement participant)
{
if (participant != null)
{
Transaction transaction = GetTransaction(transactionName);
if (transaction != null)
{
transaction.Remove(participant);
}
}
}
public static bool Suspended
{
get { return uSession.Context.TransSuspended; }
set { uSession.Context.TransSuspended = value; }
}
private static void VerifyTransactionActive()
{
if (uSession.Context.Transactions.Count == 0)
{
New();
}
}
private static int IndexOf(string transactionName)
{
if (string.IsNullOrEmpty(transactionName))
{
return uSession.Context.Transactions.Count - 1;
}
for (int i = uSession.Context.Transactions.Count - 1; i > 0; i--)
{
if ((uSession.Context.Transactions[i] as Transaction).Name == transactionName)
{
return i;
}
}
return -1;
}
}
}
Is the code generated properly. Please let me know if you want to see any other file from generated section
Joined: 28-Oct-2009
Hi,
I was thinking what could be the reason for thei behavior. I am a newbie to LLBLgen.
We are using SelfServicing code generated. Will this issue come because of that?
Please help me out with the same . I am completely stuck here.
Thanks Sam
Joined: 28-Nov-2005
Sam,
We will follow in this thread: http://llblgen.com/tinyforum/Messages.aspx?ThreadID=16848