init
This commit is contained in:
commit
e124a47765
19374 changed files with 9806149 additions and 0 deletions
232
Framework/Session/TransactionContext.cs
Normal file
232
Framework/Session/TransactionContext.cs
Normal file
|
@ -0,0 +1,232 @@
|
|||
using System;
|
||||
using System.Data;
|
||||
using SDA.DataProvider;
|
||||
|
||||
namespace Kreta.Framework
|
||||
{
|
||||
/// <summary>
|
||||
/// Adatbázis ügylet kontextus.
|
||||
/// </summary>
|
||||
public class TransactionContext : IDisposable
|
||||
{
|
||||
private static object SyncObject = new object();
|
||||
|
||||
private SDAConnection dbConnection;
|
||||
|
||||
private SDATransaction dbTransaction;
|
||||
|
||||
private bool IsTransactional { get; set; }
|
||||
|
||||
private bool IsLocked { get; set; }
|
||||
|
||||
protected string IntezmenyAzonosito { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Az osztály alapértelmezett konstruktora.
|
||||
/// </summary>
|
||||
public TransactionContext(string intezmenyAzonosito) : this(intezmenyAzonosito, true)
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Az osztály konstruktora.
|
||||
/// </summary>
|
||||
/// <param name="isTransactional">Tranzakciós legyen-e a kontextus, vagy sem.</param>
|
||||
public TransactionContext(string intezmenyAzonosito, bool isTransactional)
|
||||
{
|
||||
IsTransactional = isTransactional;
|
||||
IntezmenyAzonosito = intezmenyAzonosito;
|
||||
}
|
||||
|
||||
#region Belső dolgok
|
||||
|
||||
internal virtual SDAConnection CreateConnection()
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(IntezmenyAzonosito))
|
||||
{
|
||||
var connectionString = SDAServer.Instance.GetIntezmenyConnectionString(IntezmenyAzonosito);
|
||||
|
||||
return SDAServer.Instance.CreateConnection(connectionString);
|
||||
}
|
||||
|
||||
return SDAServer.Instance.CreateConnection();
|
||||
}
|
||||
|
||||
private void Prepare()
|
||||
{
|
||||
DoPrepareConnection();
|
||||
DoPrepareTransaction();
|
||||
}
|
||||
|
||||
private void DoPrepareConnection()
|
||||
{
|
||||
if (dbConnection == null)
|
||||
{
|
||||
dbConnection = CreateConnection();
|
||||
dbConnection.Open();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (dbConnection.State == ConnectionState.Closed)
|
||||
{
|
||||
// Ide nem kéne bejönnie...
|
||||
dbConnection.Open();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void DoPrepareTransaction()
|
||||
{
|
||||
if (IsTransactional && dbTransaction == null)
|
||||
{
|
||||
dbTransaction = dbConnection.BeginTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
private void DisposeTransaction()
|
||||
{
|
||||
if (dbTransaction != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
dbTransaction.Dispose();
|
||||
}
|
||||
finally
|
||||
{
|
||||
dbTransaction = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void CloseLocked()
|
||||
{
|
||||
}
|
||||
|
||||
private void CloseNotLocked()
|
||||
{
|
||||
DisposeTransaction();
|
||||
if (dbConnection != null)
|
||||
{
|
||||
try
|
||||
{
|
||||
dbConnection.Dispose();
|
||||
}
|
||||
finally
|
||||
{
|
||||
dbConnection = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Nyilvános felület
|
||||
|
||||
/// <summary>
|
||||
/// Zárolja a munkamenet ügyletét, így a munkamenet passziválásakor nem fog elveszni.
|
||||
/// </summary>
|
||||
public bool Lock()
|
||||
{
|
||||
lock (SyncObject)
|
||||
{
|
||||
if (IsLocked)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
IsLocked = true;
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Feloldja a munkamenet ügyletének zárolását.
|
||||
/// </summary>
|
||||
public void Unlock()
|
||||
{
|
||||
lock (SyncObject)
|
||||
{
|
||||
IsLocked = false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A munkamenethez tartozó adatbáziskapcsolat.
|
||||
/// </summary>
|
||||
public SDAConnection DBConnection
|
||||
{
|
||||
get
|
||||
{
|
||||
Prepare();
|
||||
return dbConnection;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A munkamenethez tartozó ügylet.
|
||||
/// </summary>
|
||||
public SDATransaction DBTransaction
|
||||
{
|
||||
get
|
||||
{
|
||||
Prepare();
|
||||
return dbTransaction;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Jóváhagyja az ügyletet.
|
||||
/// </summary>
|
||||
public void Commit()
|
||||
{
|
||||
if (IsTransactional && !IsLocked && dbTransaction != null)
|
||||
{
|
||||
dbTransaction.Commit();
|
||||
DisposeTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Visszagörgeti az ügyletet.
|
||||
/// </summary>
|
||||
public void Rollback()
|
||||
{
|
||||
if (IsTransactional && dbTransaction != null)
|
||||
{
|
||||
dbTransaction.Rollback();
|
||||
DisposeTransaction();
|
||||
DoPrepareTransaction();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lezárja a kontextust.
|
||||
/// </summary>
|
||||
public void Close()
|
||||
{
|
||||
if (dbConnection == null && dbTransaction == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (IsLocked)
|
||||
{
|
||||
CloseLocked();
|
||||
}
|
||||
else
|
||||
{
|
||||
CloseNotLocked();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Lezárja a kontextust.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
Close();
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue