Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/03/09 11:54:10 (15 years ago)
Author:
svonolfe
Message:

Added thread checking in session (#527)

File:
1 edited

Legend:

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

    r1488 r1496  
    2727using HeuristicLab.PluginInfrastructure;
    2828using System.Data.Common;
     29using System.Threading;
    2930
    3031namespace HeuristicLab.DataAccess.ADOHelper {
     
    3940    private DbConnection connection;
    4041
     42    private Thread ownerThread;
     43
    4144    private IDictionary<Guid, object> adapters =
    4245      new Dictionary<Guid, object>();
     
    4447    public Session(SessionFactory factory) {
    4548      this.factory = factory;
     49      this.ownerThread = Thread.CurrentThread;
     50    }
     51
     52    public void CheckThread() {
     53      if (!Thread.CurrentThread.Equals(ownerThread)) {
     54        throw new Exception("Session is owned by another thread");
     55      }
    4656    }
    4757
     
    6272    #region ISession Members
    6373    public ITransaction BeginTransaction() {
     74      CheckThread();
     75
    6476      if (transaction == null) {
    6577         transaction = new Transaction(this);
     
    7183
    7284    public ITransaction GetCurrentTransaction() {
     85      CheckThread();
     86
    7387      return transaction;
    7488    }
     
    7690    public IDataAdapter<ObjT> GetDataAdapter<ObjT>()
    7791      where ObjT : IPersistableObject {
     92      CheckThread();
     93
    7894      Guid adapterId = typeof(IDataAdapter<ObjT>).GUID;
    7995
     
    94110      where T : class, IDataAdapter<ObjT>
    95111    {
    96         Guid adapterId = typeof(T).GUID;
     112      CheckThread();
    97113
    98         if (!adapters.ContainsKey(adapterId)) {
    99           T adapter =
    100             discoveryService.GetInstances<T>()[0];
     114      Guid adapterId = typeof(T).GUID;
    101115
    102           adapter.Session = this;
     116      if (!adapters.ContainsKey(adapterId)) {
     117        T adapter =
     118          discoveryService.GetInstances<T>()[0];
    103119
    104           adapters.Add(adapterId, adapter);
    105         }
     120        adapter.Session = this;
    106121
    107         return adapters[adapterId] as T;
     122        adapters.Add(adapterId, adapter);
     123      }
     124
     125      return adapters[adapterId] as T;
    108126    }
    109127
    110128    public void EndSession() {
     129      CheckThread();
     130
    111131      if (transaction != null) {
    112132        transaction.Rollback();
Note: See TracChangeset for help on using the changeset viewer.