Changeset 1488


Ignore:
Timestamp:
04/02/09 15:00:35 (11 years ago)
Author:
svonolfe
Message:

Updated transaction management (#527)

Location:
trunk/sources
Files:
1 deleted
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.DataAccess.ADOHelper/DataAdapterBase.cs

    r1468 r1488  
    144144    protected ObjT FindSingle(Selector selector) {
    145145      ITransaction trans =
    146        session.GetTransactionForCurrentThread();
     146       session.GetCurrentTransaction();
    147147      bool transactionExists = trans != null;
    148148      if (!transactionExists) {
     
    174174    protected ICollection<ObjT> FindMultiple(Selector selector) {
    175175      ITransaction trans =
    176        session.GetTransactionForCurrentThread();
     176       session.GetCurrentTransaction();
    177177      bool transactionExists = trans != null;
    178178      if (!transactionExists) {
     
    242242    public void Update(ObjT obj) {
    243243      ITransaction trans =
    244         session.GetTransactionForCurrentThread();
     244        session.GetCurrentTransaction();
    245245      bool transactionExists = trans != null;
    246246      if (!transactionExists) {
     
    294294    public bool Delete(ObjT obj) {
    295295      ITransaction trans =
    296         session.GetTransactionForCurrentThread();
     296        session.GetCurrentTransaction();
    297297      bool transactionExists = trans != null;
    298298      if (!transactionExists) {
  • trunk/sources/HeuristicLab.DataAccess.ADOHelper/DataAdapterWrapperBase.cs

    r1468 r1488  
    4343      get {
    4444        ITransaction trans =
    45           session.GetTransactionForCurrentThread();
     45          session.GetCurrentTransaction();
    4646        if (trans != null)
    4747          SetTransaction(trans.InnerTransaction as DbTransaction);
  • trunk/sources/HeuristicLab.DataAccess.ADOHelper/HeuristicLab.DataAccess.ADOHelper.csproj

    r1486 r1488  
    8888    <Compile Include="SessionFactory.cs" />
    8989    <Compile Include="Transaction.cs" />
    90     <Compile Include="TransactionManager.cs" />
    9190  </ItemGroup>
    9291  <ItemGroup>
  • trunk/sources/HeuristicLab.DataAccess.ADOHelper/Session.cs

    r1468 r1488  
    3535    private SessionFactory factory;
    3636
    37     private ITransactionManager transManager;
     37    private Transaction transaction;
    3838
    3939    private DbConnection connection;
     
    4242      new Dictionary<Guid, object>();
    4343
    44     public Session(SessionFactory factory,
    45       ITransactionManager transManager) {
     44    public Session(SessionFactory factory) {
    4645      this.factory = factory;
    47       this.transManager = transManager;
    4846    }
    4947
     
    5856    }
    5957
     58    public void DetachTrasaction() {
     59      this.transaction = null;
     60    }
     61
    6062    #region ISession Members
    6163    public ITransaction BeginTransaction() {
    62       ITransaction trans = transManager.BeginTransaction();
    63       if(trans is Transaction)
    64         ((Transaction)trans).Connection = Connection;
     64      if (transaction == null) {
     65         transaction = new Transaction(this);
     66         transaction.Connection = Connection;
     67      }
    6568
    66       return trans;
     69      return transaction;
    6770    }
    6871
    69     public ITransaction GetTransactionForCurrentThread() {
    70       ITransaction trans = transManager.GetTransactionForCurrentThread();
    71       if (trans != null && trans is Transaction)
    72         ((Transaction)trans).Connection = Connection;
    73 
    74       return trans;
     72    public ITransaction GetCurrentTransaction() {
     73      return transaction;
    7574    }
    7675
    7776    public IDataAdapter<ObjT> GetDataAdapter<ObjT>()
    7877      where ObjT : IPersistableObject {
    79       lock(this) {
    80         Guid adapterId = typeof(IDataAdapter<ObjT>).GUID;
     78      Guid adapterId = typeof(IDataAdapter<ObjT>).GUID;
    8179
    82         if (!adapters.ContainsKey(adapterId)) {
    83           IDataAdapter<ObjT> adapter =
    84             discoveryService.GetInstances<IDataAdapter<ObjT>>()[0];
     80      if (!adapters.ContainsKey(adapterId)) {
     81        IDataAdapter<ObjT> adapter =
     82          discoveryService.GetInstances<IDataAdapter<ObjT>>()[0];
    8583
    86           adapter.Session = this;
     84        adapter.Session = this;
    8785
    88           adapters.Add(adapterId, adapter);
    89         }
     86        adapters.Add(adapterId, adapter);
     87      }
    9088
    91         return adapters[adapterId] as IDataAdapter<ObjT>;
    92       }
     89      return adapters[adapterId] as IDataAdapter<ObjT>;
    9390    }
    9491
     
    9794      where T : class, IDataAdapter<ObjT>
    9895    {
    99       lock (this) {
    10096        Guid adapterId = typeof(T).GUID;
    10197
     
    110106
    111107        return adapters[adapterId] as T;
    112       }
    113108    }
    114109
    115110    public void EndSession() {
    116       if(connection.State == System.Data.ConnectionState.Open)
     111      if (transaction != null) {
     112        transaction.Rollback();
     113        transaction = null;
     114      }
     115      if (connection.State == System.Data.ConnectionState.Open) {
    117116        connection.Close();
     117        connection = null;
     118      }
    118119      factory.EndSession(this);
    119120    }
  • trunk/sources/HeuristicLab.DataAccess.ADOHelper/SessionFactory.cs

    r1468 r1488  
    3333    private IDictionary<Thread, ISession> sessions =
    3434      new Dictionary<Thread, ISession>();
    35 
    36     private ITransactionManager transManager =
    37           new TransactionManager();
    3835
    3936    public Type DbConnectionType { get;  set; }
     
    8683        if (!sessions.ContainsKey(current)) {
    8784          sessions[current] =
    88             new Session(this, transManager);
     85            new Session(this);
    8986        }
    9087
  • trunk/sources/HeuristicLab.DataAccess.ADOHelper/Transaction.cs

    r1468 r1488  
    3131  class Transaction: ITransaction {
    3232    private DbTransaction transaction;
    33     private TransactionManager manager;
     33
     34    private Session session;
     35
     36    public Transaction(Session session) {
     37      this.session = session;
     38    }
    3439
    3540    #region ITransaction Members
    36 
    37     public Transaction(TransactionManager manager) {
    38       this.manager = manager;
    39     }
    40 
    4141    public DbConnection Connection {
    4242      set {
     
    5454
    5555    public void Commit() {
    56       manager.RemoveTransaction(this);
    5756      if (transaction != null) {
    5857        DbConnection conn =
     
    6059
    6160        transaction.Commit();
     61
     62        transaction = null;
     63        session.DetachTrasaction();
    6264
    6365        if (conn != null &&
     
    6870
    6971    public void Rollback() {
    70       manager.RemoveTransaction(this);
    71       DbConnection conn =
    72           transaction.Connection;
     72      if (transaction != null) {
     73        DbConnection conn =
     74            transaction.Connection;
    7375
    74       transaction.Rollback();
     76        transaction.Rollback();
    7577
    76       if (conn != null &&
    77           conn.State == System.Data.ConnectionState.Open)
    78         conn.Close();
     78        transaction = null;
     79        session.DetachTrasaction();
     80
     81        if (conn != null &&
     82            conn.State == System.Data.ConnectionState.Open)
     83          conn.Close();
     84      }
    7985    }
    8086
  • trunk/sources/HeuristicLab.DataAccess/Interfaces/ISession.cs

    r1468 r1488  
    2929    ITransaction BeginTransaction();
    3030
    31     ITransaction GetTransactionForCurrentThread();
     31    ITransaction GetCurrentTransaction();
    3232
    3333    IDataAdapter<ObjT> GetDataAdapter<ObjT>()
  • trunk/sources/HeuristicLab.Security.DataAccess/Properties/AssemblyInfo.frame

    r1378 r1488  
    2828// set of attributes. Change these attribute values to modify the information
    2929// associated with an assembly.
    30 [assembly: AssemblyTitle("HeuristicLab.Security.DataAccess)]
     30[assembly: AssemblyTitle("HeuristicLab.Security.DataAccess")]
    3131[assembly: AssemblyDescription("")]
    3232[assembly: AssemblyConfiguration("")]
Note: See TracChangeset for help on using the changeset viewer.