Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SessionScope"/> class.
        /// </summary>
        /// <param name="settings">An <see cref="SessionScopeSettings"/> instance holding the scope configuration</param>
        /// <param name="open">
        /// If set to <c>true</c> associate a session with the thread.  If false, another
        /// collaborating class will associate the session with the thread, potentially by calling
        /// the Open method on this class.
        /// </param>
        public SessionScope(SessionScopeSettings settings, bool open)
        {
            log           = LogManager.GetLogger(this.GetType());
            this.settings = settings;

            PARTICIPATE_KEY = UniqueKey.GetInstanceScopedString(this, "Participate");
            ISOPEN_KEY      = UniqueKey.GetInstanceScopedString(this, "IsOpen");

            if (open)
            {
                Open();
            }
        }
        public void WorksAsExpected()
        {
            ISessionFactory sessionFactory    = A.Fake <ISessionFactory>();
            IInterceptor    entityInterceptor = A.Fake <IInterceptor>();

            Assert.AreNotEqual(FlushMode.Auto, SessionScopeSettings.FLUSHMODE_DEFAULT); // ensure noone changed our assumptions
            SessionScopeSettings sss = new SessionScopeSettings(sessionFactory, entityInterceptor, !SessionScopeSettings.SINGLESESSION_DEFAULT, FlushMode.Auto);

            Assert.AreEqual(sessionFactory, sss.SessionFactory);
            Assert.AreEqual(entityInterceptor, sss.EntityInterceptor);
            Assert.AreEqual(!SessionScopeSettings.SINGLESESSION_DEFAULT, sss.SingleSession);
            Assert.AreEqual(FlushMode.Auto, sss.DefaultFlushMode);
        }
        public void WorksAsExpected()
        {
            MockRepository  mocks             = new MockRepository();
            ISessionFactory sessionFactory    = (ISessionFactory)mocks.CreateMock(typeof(ISessionFactory));
            IInterceptor    entityInterceptor = (IInterceptor)mocks.CreateMock(typeof(IInterceptor));

            Assert.AreNotEqual(FlushMode.Auto, SessionScopeSettings.FLUSHMODE_DEFAULT);   // ensure noone changed our assumptions
            SessionScopeSettings sss = new SessionScopeSettings(sessionFactory, entityInterceptor, !SessionScopeSettings.SINGLESESSION_DEFAULT, FlushMode.Auto);

            Assert.AreEqual(sessionFactory, sss.SessionFactory);
            Assert.AreEqual(entityInterceptor, sss.EntityInterceptor);
            Assert.AreEqual(!SessionScopeSettings.SINGLESESSION_DEFAULT, sss.SingleSession);
            Assert.AreEqual(FlushMode.Auto, sss.DefaultFlushMode);
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SessionScope"/> class.
        /// </summary>
        /// <param name="settings">An <see cref="SessionScopeSettings"/> instance holding the scope configuration</param>
        /// <param name="open">
        /// If set to <c>true</c> associate a session with the thread.  If false, another
        /// collaborating class will associate the session with the thread, potentially by calling
        /// the Open method on this class.
        /// </param>
        public SessionScope(SessionScopeSettings settings, bool open)
        {
            log = LogManager.GetLogger(this.GetType());
            this.settings = settings;

            PARTICIPATE_KEY = UniqueKey.GetInstanceScopedString(this, "Participate");
            ISOPEN_KEY = UniqueKey.GetInstanceScopedString(this, "IsOpen");

            if (open)
            {
                Open();
            }
        }
    /// <summary>
    /// Test with conversation and "connection.release_mode" 
    /// "auto"(<see cref="ConnectionReleaseMode.AfterTransaction"/>).
    /// </summary>
    /// <remarks>
    /// Here we can see that every statement causes a closing of the IDbConnection.
    /// </remarks>
    private void connection_release_mode_auto()
    {
        //with conversation and "connection.release_mode" "auto"(AfterTransaction)
        //forcing "auto" by reflection.
        Settings settings = ((SessionFactoryImpl) this.SessionFactory).Settings;
        ConnectionReleaseMode connReleaseModeOriginal = settings.ConnectionReleaseMode;
        this.setConnectionReleaseModeByReflection(settings, ConnectionReleaseMode.AfterTransaction);

        //((SessionFactoryImpl)this.sessionFactory).Settings.ConnectionReleaseMode = ConnectionReleaseMode.AfterTransaction;
        ConnectionCreationTrackingDbProvider.Count = 0;
        this.Conversation.StartResumeConversation();
        ISession sessionA = this.SessionFactory.GetCurrentSession();
        SPCDetailEnt detailEnt = sessionA.Get<SPCDetailEnt>(1);
        Assert.AreEqual(1, ConnectionCreationTrackingDbProvider.Count, "ConnectionCreationTrackingDbProvider.Count");

        SessionScopeSettings sessionScopeSettings = new SessionScopeSettings(this.sessionFactory);
        sessionScopeSettings.SingleSession = true;
        using (new SessionScope(sessionScopeSettings, false))
        {
            ISession sessionB = this.SessionFactory.GetCurrentSession();

            sessionB.Get<SPCMasterEnt>(1);
            Assert.AreEqual(2, ConnectionCreationTrackingDbProvider.Count, "ConnectionCreationTrackingDbProvider.Count");

            Assert.AreSame(sessionA, sessionB, "sessionA, sessionB");
        }

        SPCMasterEnt masterEnt2 = sessionA.Get<SPCMasterEnt>(2);
        Assert.AreEqual(3, ConnectionCreationTrackingDbProvider.Count, "ConnectionCreationTrackingDbProvider.Count");
        Assert.AreEqual(1, masterEnt2.SPCDetailEntList.Count, "masterEnt2.SPCDetailEntList.Count");
        Assert.AreEqual(4, ConnectionCreationTrackingDbProvider.Count, "ConnectionCreationTrackingDbProvider.Count");

        SPCMasterEnt masterEnt3 = sessionA.Get<SPCMasterEnt>(3);
        Assert.AreEqual(5, ConnectionCreationTrackingDbProvider.Count, "ConnectionCreationTrackingDbProvider.Count");
        Assert.AreEqual(1, masterEnt3.SPCDetailEntList.Count, "masterEnt3.SPCDetailEntList.Count");
        Assert.AreEqual(6, ConnectionCreationTrackingDbProvider.Count, "ConnectionCreationTrackingDbProvider.Count");

        //Renew the conversation.
        this.Conversation.EndConversation();
        this.Conversation.ConversationManager.FreeEnded();
        this.Conversation = (IConversationState) this.applicationContext.GetObject("convConnectionReleaseModeIssue");

        this.setConnectionReleaseModeByReflection(settings, connReleaseModeOriginal);
    }
    /// <summary>
    /// Test with NO conversation and "connection.release_mode" 
    /// "auto"(<see cref="ConnectionReleaseMode.AfterTransaction"/>).
    /// </summary>
    private void connection_release_mode_auto_no_conversation()
    {
        //with NO conversation and "connection.release_mode" "auto"(AfterTransaction)
        //forcing "auto" by reflection.
        Settings settings = ((SessionFactoryImpl) this.SessionFactory).Settings;
        ConnectionReleaseMode connReleaseModeOriginal = settings.ConnectionReleaseMode;
        this.setConnectionReleaseModeByReflection(settings, ConnectionReleaseMode.AfterTransaction);

        //with no conversation
        SessionScopeSettings sessionScopeSettings = new SessionScopeSettings(this.sessionFactory);
        ConnectionCreationTrackingDbProvider.Count = 0;
        using (new SessionScope(sessionScopeSettings, true))
        {
            ISession sessionNoConv = this.SessionFactory.GetCurrentSession();
            SPCMasterEnt masterEnt2 = sessionNoConv.Get<SPCMasterEnt>(2);
            Assert.AreEqual(1, ConnectionCreationTrackingDbProvider.Count, "ConnectionCreationTrackingDbProvider.Count");
            Assert.AreEqual(1, masterEnt2.SPCDetailEntList.Count, "masterEnt2.SPCDetailEntList.Count");
            Assert.AreEqual(2, ConnectionCreationTrackingDbProvider.Count, "ConnectionCreationTrackingDbProvider.Count");

            SPCMasterEnt masterEnt3 = sessionNoConv.Get<SPCMasterEnt>(3);
            Assert.AreEqual(3, ConnectionCreationTrackingDbProvider.Count, "ConnectionCreationTrackingDbProvider.Count");
            Assert.AreEqual(1, masterEnt3.SPCDetailEntList.Count, "masterEnt3.SPCDetailEntList.Count");
            Assert.AreEqual(4, ConnectionCreationTrackingDbProvider.Count, "ConnectionCreationTrackingDbProvider.Count");
        }

        this.setConnectionReleaseModeByReflection(settings, connReleaseModeOriginal);
    }
        public void WorksAsExpected()
        {
            MockRepository mocks = new MockRepository();
            ISessionFactory sessionFactory =  mocks.StrictMock<ISessionFactory>();
            IInterceptor entityInterceptor = mocks.StrictMock<IInterceptor>();
            Assert.AreNotEqual( FlushMode.Auto, SessionScopeSettings.FLUSHMODE_DEFAULT ); // ensure noone changed our assumptions
            SessionScopeSettings sss = new SessionScopeSettings(sessionFactory, entityInterceptor, !SessionScopeSettings.SINGLESESSION_DEFAULT, FlushMode.Auto );   

            Assert.AreEqual( sessionFactory, sss.SessionFactory );
            Assert.AreEqual( entityInterceptor, sss.EntityInterceptor);
            Assert.AreEqual( !SessionScopeSettings.SINGLESESSION_DEFAULT, sss.SingleSession );
            Assert.AreEqual( FlushMode.Auto, sss.DefaultFlushMode );
        }
Esempio n. 8
0
    private void participatingHibernateNotAlowed()
    {
        Regex msgErrorRx = new Regex(".*Participating.*Hibernate.*NOT.*ALLOWED.*");
        try
        {
            //No raise
            this.ConversationA.StartResumeConversation();
            ISession session = this.SessionFactory.GetCurrentSession();
            this.ConversationManager.FreeEnded();
            this.ConversationManager.PauseConversations();
            this.Session["testResult"] = "OK";
        }
        finally
        {
            this.ConversationManager.FreeEnded();
            this.ConversationManager.PauseConversations();
        }

        try
        {
            //Raise
            this.ConversationA.StartResumeConversation();
            this.ConversationManager.FreeEnded();
            this.ConversationManager.PauseConversations();

            SessionScopeSettings sessionScopeSettings = new SessionScopeSettings(this.sessionFactory);
            sessionScopeSettings.SingleSession = true;
            using (new SessionScope(sessionScopeSettings, true))
            {
                ISession session = this.SessionFactory.GetCurrentSession();
                this.ConversationA.StartResumeConversation();
            }
            throw new Exception("NOT OK: No raise for 'this.ConversationA.StartResumeConversation()'");
        }
        catch (InvalidOperationException ioe)
        {
            if (msgErrorRx.IsMatch(ioe.Message))
            {
                this.Session["testResult"] = "OK";
            }
            else
            {
                throw new Exception("NOT OK " + ioe.Message);
            }
        }
        finally
        {
            this.ConversationManager.FreeEnded();
            this.ConversationManager.PauseConversations();
        }
        
    }