Пример #1
0
        void ReleaseSessionImpl(SerializerSessionImpl wrapper)
        {
            SerializerSession session = wrapper.session;

            if (null == session)
            {
                throw new ArgumentNullException("session");
            }

            if (session.isInTransaction)
            {
                throw new Exception("You must either commit or rollback the current transaction before releasing the session.");
            }

            sessionCooperativeLockLeave(wrapper);

            session.clearThread();

            if (singleSessionPerThread)
            {
                threadSession = null;
            }

            lock (this.syncRoot)
                m_freeSessions.Push(session);

            if (null != m_semaphore)
            {
                m_semaphore.Release();
            }
        }
Пример #2
0
        private void sessionCooperativeLockLeave(SerializerSessionImpl sess)
        {
            switch (sess.cooperativeLevel)
            {
            case eSessionCooperativeLevel.NonExclusive:
                this.sessionCooperationLock.ExitReadLock();
                return;

            case eSessionCooperativeLevel.Exclusive:
                this.sessionCooperationLock.ExitWriteLock();
                return;
            }
        }
Пример #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);
        }