DifferentDatabaseScope not using different database

11 views
Skip to first unread message

Andy

unread,
Nov 5, 2009, 2:50:47 PM11/5/09
to Castle Project Users
Hi,

I have a problem with the (I know, still very experimental)
DifferentDatabaseScope class. Here is my code (or at least a bit of
it):

using (IDbConnection conn = GetMyDbConnection())
{
conn.Open();

using (var diffScope = new DifferentDatabaseScope(conn))
{
// ...
orders = ShopOrders.FindAll();
// ...
}
}

The conn.Open() line works, the connection is opened and works (I
checked that from the server-side). But at the "FindAll" line, an
exception is thrown, with an inner exception saying that the user
"user@server" is not allowed to SELECT. But the main problem is that
the user name mentioned in the exception is the wrong one, namely the
one defined in my AR configuration, and not the one specified in the
connection (string) coming from "GetMyDbConnection".

So why is AR using the "default" connection instead of the "different"
given connection? I have no more ideas.

Thanks in advance.

Greetings from Germany - Andy

Markus Zywitza

unread,
Nov 6, 2009, 12:08:19 PM11/6/09
to castle-pro...@googlegroups.com
The DifferentDatabaseScope is not meant to be experimental. From what
you write, I think AR is using the connection. If it used the main
connection, how could it fail with this connection's credentials?

So my guess is, the connection is used, but somehow AR/NH tries to
re-open the connection with the wrong credentials.

Can you create a Donjon issue with a failing test or example project?

-Markus

2009/11/5 Andy <a.bon...@gmx.de>:

Andy

unread,
Nov 6, 2009, 6:44:38 PM11/6/09
to Castle Project Users
First of all - thanks for your reply.

In the following text, user1 can access host1 hosting db1 which
contains table1, and the same for user2, host2, db2 and table2.

So, sorry for that. I accidentally forgot that I already changed my
code because I was trying some more or less useful things for
bugtracking.

The original code produces a different error: "Table db1.table2 does
not exist." This is true, of course - but the error only occurs
because AR is using the wrong db connection (namely a connection to
host1 which has db1 as default schema). But table2 is in db2 on host2,
and I can reach host2 only by using the "different database
connection" because I MUST close the connection to host2 after the
transaction immediately.

So I came to the conclusion that AR still uses the existing default
connection (to db1) and tries to access table2 instead of using the
"different database" to do this. And THAT is my problem.

- Andy

P.S.: The error that I mentioned my original post occurs when I
specify the correct schema in the AR table class of table2. But this
schema (db2) does not exist on host1 (to which AR connects wrongly).
And for some weird reason, MySQL says that the user1 is not allowed to
SELECT from db2.table2 on host1 - but the correct error message would
be "database db2 does not exist on host1".

On 6 Nov., 18:08, Markus Zywitza <markus.zywi...@gmail.com> wrote:
> The DifferentDatabaseScope is not meant to be experimental. From what
> you write, I think AR is using the connection. If it used the main
> connection, how could it fail with this connection's credentials?
>
> So my guess is, the connection is used, but somehow AR/NH tries to
> re-open the connection with the wrong credentials.
>
> Can you create a Donjon issue with a failing test or example project?
>
> -Markus
>
> 2009/11/5 Andy <a.bonga...@gmx.de>:

Andy

unread,
Nov 13, 2009, 12:50:29 PM11/13/09
to Castle Project Users
OK, I know what the problem is/was. Using a DifferentDatabaseScope is
not thread-safe. My own FindAll method overrides the one from
ActiveRecordBase. It starts a thread that executes the inherited
FindAll. But in this thread the DifferentDatabaseScope that I created
outside is not available.

Markus Zywitza

unread,
Nov 15, 2009, 10:01:15 AM11/15/09
to castle-pro...@googlegroups.com
Good you found the error. I have been searching the code, not being
able to create a failing a test for your problem.

-Markus

2009/11/13 Andy <a.bon...@gmx.de>:
> --
>
> You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
> To post to this group, send email to castle-pro...@googlegroups.com.
> To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=.
>
>
>
Reply all
Reply to author
Forward
0 new messages