Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1496


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

Added thread checking in session (#527)

Location:
trunk/sources/HeuristicLab.DataAccess.ADOHelper
Files:
2 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();
  • trunk/sources/HeuristicLab.DataAccess.ADOHelper/Transaction.cs

    r1488 r1496  
    3838    }
    3939
    40     #region ITransaction Members
    4140    public DbConnection Connection {
    4241      set {
    4342        if (value != null &&
    4443          (transaction == null ||
    45            !(transaction.Connection != null && 
     44           !(transaction.Connection != null &&
    4645             transaction.Connection.Equals(value)))) {
    47             if (value.State != System.Data.ConnectionState.Open)
    48               value.Open();
     46          if (value.State != System.Data.ConnectionState.Open)
     47            value.Open();
    4948
    50             transaction = value.BeginTransaction(IsolationLevel.RepeatableRead);
     49          transaction = value.BeginTransaction(IsolationLevel.RepeatableRead);
    5150        }
    5251      }
    5352    }
    5453
     54    #region ITransaction Members
    5555    public void Commit() {
     56      this.session.CheckThread();
     57
    5658      if (transaction != null) {
    5759        DbConnection conn =
     
    7072
    7173    public void Rollback() {
     74      this.session.CheckThread();
     75
    7276      if (transaction != null) {
    7377        DbConnection conn =
     
    8791    public object InnerTransaction {
    8892      get {
     93        this.session.CheckThread();
     94
    8995        return transaction;
    9096      }
Note: See TracChangeset for help on using the changeset viewer.