Ejemplo n.º 1
0
 public SerializerSessionImpl(SerializerSession session, SessionPool pool, eSessionCooperativeLevel coopLevel)
 {
     this.pool             = pool;
     this.session          = session;
     refCount              = 0;
     this.cooperativeLevel = coopLevel;
 }
Ejemplo n.º 2
0
 /// <summary>Get the ESENT session from the pool.</summary>
 /// <param name="coopLevel">Specify whether an exclusive session is requested, the default is NonExclusive.</param>
 /// <returns>Return the session.</returns>
 /// <remarks>
 /// <para>This operation is inexpensive: the method usually returns the last session released to the pool.</para>
 /// <para><b>NB:</b> You must call <see cref="IDisposable.Dispose">Dispose</see> exactly once
 /// for each iSerializerSession instance returned by this method. The recommended construction is:</para>
 /// <code lang="C#">            using( iSerializerSession sess = m_sessionPool.GetSession() )
 ///using( iSerializerTransaction trans = sess.beginTransaction() )
 ///{
 ///	// Do whatever you need with the data: obtain cursors/recordsets, search, modify records, etc..
 ///	trans.Commit();		// Or don't, if you need the transaction to roll back.
 ///}</code>
 /// </remarks>
 public iSerializerSession GetSession(eSessionCooperativeLevel coopLevel = eSessionCooperativeLevel.NonExclusive)
 {
     return(GetSessionImpl(coopLevel));
 }
Ejemplo n.º 3
0
        iSerializerSession GetSessionImpl(eSessionCooperativeLevel coopLevel)
        {
            if (singleSessionPerThread && null != threadSession)
            {
                if (threadSession.cooperativeLevel != coopLevel)
                {
                    throw new Exception("You can't get sessions with different cooperative levels on the same thread at the same time");
                }
                threadSession.AddRef();
                return(threadSession);
            }

            if (null != m_semaphore)
            {
                if (!m_semaphore.WaitOne(tsWaitForFreeSession))
                {
                    throw new TimeoutException("SessionPool.tsWaitForFreeSession timeout exceeded.");
                }
            }

            SerializerSession nativeSession = null;
            bool bNewSession = false;

            lock (this.syncRoot)
            {
                if (m_freeSessions.Count > 0)
                {
                    nativeSession = m_freeSessions.Pop();
                }
                else
                {
                    m_serializer.EnsureDatabaseExists();

                    nativeSession = new SerializerSession(m_serializer);
                    m_allSessions.Add(nativeSession);
                    bNewSession = true;

                    if (m_bFirstSession)
                    {
                        m_bFirstSession = false;

                        if (null != this.updateSchema)
                        {
                            DatabaseSchemaUpdater updater = new DatabaseSchemaUpdater(nativeSession);
                            this.updateSchema(updater);
                        }
                    }
                }
            }

            if (bNewSession)
            {
                foreach (var t in m_recordTypes)
                {
                    nativeSession.AddType(t);
                }
                TraceInfo("GetSessionImpl: constructed a new session");
            }

            SerializerSessionImpl sessionWrapper = new SerializerSessionImpl(nativeSession, this, coopLevel);

            sessionCooperativeLockEnter(sessionWrapper);
            sessionWrapper.AddRef();

            if (singleSessionPerThread)
            {
                threadSession = sessionWrapper;
            }

            // Bind new session to the current thread
            nativeSession.setThread();

            return(sessionWrapper);
        }