No, not if the connection was provided by NH.

In the code example of your initial post, you managed the connection
explicitly. Why is it a problem then to pass an open session to
DifferentDatabaseScope?

-Markus

2009/8/11 EricP <[email protected]>

>
> Another approach I tried was getting connection directly from
> ActiveRecord using code:
>
> ===========
>                       using (var sessionScope = new SessionScope())
>                        {
>                                IDbConnection cn =
> ActiveRecordMediator.GetSessionFactoryHolder()
>
>    .CreateSession(typeof(object))
>
>    .Connection;
>
>                                Assert.AreEqual(cn.State,
> ConnectionState.Open);
>
> ============
>
>
> But once again the connection returned by CreateSession(typeof
> (object)).Connection is Open.  Is there a way to get a connection from
> AR without it being Open.
>
> Eric
>
> On Aug 7, 3:20 pm, EricP <[email protected]> wrote:
> > Thank you for suggestion.
> >
> > I tried out DifferentDatabaseScope and it does seem to work.  I did
> > find an issue.  DifferentDatabaseScope seems to require an open
> > connection.
> >
> > I wrote a simple test:
> >
> > ======================================
> > public void DifferentDatabaseScope_Requires_Open_Connection()
> > {
> >         IDbConnection cn = XConnectionFactory.GetCn();  //Not opened
> >
> >         using (new DifferentDatabaseScope(cn))
> >         {
> >                 var Customer = new Customer(...);
> >                 customer.Save();    //Get exceptioin
> >         }
> >
> > }
> >
> > ===================================
> >
> > On the line with "customer.Save()" - there is exception:
> > System.InvalidOperationException: ExecuteReader requires an open and
> > available Connection. The connection's current state is closed.
> >
> > Since I use SessionScope per HTTP request approach, I think it would
> > be better not to keep the connection open during the duration of the
> > Http Request.  SessionScope seems to only open the connection when it
> > is needed.  Is there a way to use DifferentDatabaseScope in the same
> > way?
> >
> > Eric
> >
> > On Aug 6, 11:44 pm, Mauricio Scheffer <[email protected]>
> > wrote:
> >
> > > Take a look at DifferentDatabaseScope.
> >
> > > Here's the testcase for reference:
> https://svn.castleproject.org/svn/castle/trunk/ActiveRecord/Castle.Ac...
> >
> > > On Aug 6, 10:43 pm, EricP <[email protected]> wrote:
> >
> > > > In my project we have some older modules written using straight
> > > > ADO.NET <http://ado.net/> and some newer modules that use Castle
> Active Record.  In
> > > > several places where ADO.NET <http://ado.net/> code and NHibernate
> code are used in the
> > > > same TransactionScope - I started getting errors that MSDTC is not
> > > > available.
> >
> > > > Here is the sample code that results in exception:
> >
> > > > ===========================
> >
> > > > using (var scope=System.Transactions.TransactionScope())
> > > > {
> >
> > > >    using(var connection = new SqlConnection(connectionString))
> > > >    {
> > > >         cn.Open();
> >
> > > >         //Make some changes using ADO.NET <http://ado.net/>
> > > >         string insertQuery = @"insert into employee ....";
> > > >         SqlCommand cmd = new SqlCommand(insertQuery, cn);
> > > >         cmd.ExecuteNonQuery();
> >
> > > >         // Make some changes using AR session
> > > >         using (new SessionScope())   //Need to pass cn in here
> somehow
> > > >         {
> > > >                 var Customer = new Customer(...);
> > > >                 customer.Save();        //Get exception that "MSDTC
> on
> > > > server '...' is unavailable."
> > > >         }
> >
> > > >    }
> >
> > > >    scope.Complete();
> >
> > > > }
> >
> > > > ===================
> >
> > > > This occurs because ADO.NET <http://ado.net/> code is using one
> connection, while
> > > > ActiveRecord creates another one.
> > > > If I used NHibernate directly I could probably call
> > > > SessionFactory.OpenSession(cn); with my ADO.NET 
> > > > <http://ado.net/>connection to get
> > > > around MSDTC issue.
> >
> > > > How would I do the same for ActiveRecord?
> >
> > > > Eric
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Castle Project Users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/castle-project-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to