You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by Thomas Fischer <fi...@seitenbau.net> on 2007/04/13 11:34:23 UTC

Criteria.setDbName() and BaseXXXPeer.setDbName()

I am suggesting to change the behaviour of Criteria.setDbName() and
BaseXXXPeer.setDbName().

The problem is the following:
I have generated om classes for a database name A. This leads to
BaseXXXPeer.DATABASE_NAME being set to "A".
Now I am using these generated classes in a runtime setup where the
database B is the default database. I want to query XXX objects on database
B. So I write
Criteria criteria = new Criteria();
// fill criteria
criteria.setDbName("B");
List result = BaseXXXPeer.doSelect(criteria);

This does not do what one excpects, instead the doSelect() tries to query
the database A. This is what happens:

from BaseXXXPeer.Java

public static List doSelectVillageRecords(Criteria criteria, Connection
con)
    throws TorqueException
{
...
    setDbName(criteria);
...
    // do Select, return results
}

private static void setDbName(Criteria crit)
{
    if (crit.getDbName() == Torque.getDefaultDB())
    {
        crit.setDbName(DATABASE_NAME);
    }
}

So because B (by chance) is the default database, BaseXXXPeer decides to
replace "B" by its own database, "A", although I have told the criteria
explicitly to use the database "B".

To shed light why this strange logic is used, one can look at code in
Criteria.java:
.....
private String dbName;
....
public void setDbName(String dbName)
{
    this.dbName = (dbName == null ? Torque.getDefaultDB() : dbName.trim());
}
So if dbName is passed as null to setDbName, the db name is set to the
default DB. By this, one looses the choice of telling Torque to "always use
the default db" (by calling setDbName(Torque.getDefaultDb)) or to use
"whichever database seems most appropriate" (by calling setDbName(null)).

I think this is a very strange logic, and is certainly not what one wants.

I'd suggest at least the following change:
- if Criteria.setDbName(null) is called, criteria's DbName instance
variable should set to null, not Torque.getDefaultDb(). Null values are not
a problem, the instance variable is null anyway if it is not explicitly
set.
- in BaseXXXPeer, the check should be changed from if (crit.getDbName() ==
Torque.getDefaultDB()) to if (crit.getDbName() == null)

In a second step, one could think about the following:
- use the generated database names only to identify the correct database
maps and not for accessing the runtime configuration. In the places where
the runtime configuration is accessed by using the genereated
DATABASE_NAME, we should use Torque.getDefaultDB() instead of
DATABASE_NAME.

I'm not sure whether step 1 should be included in RC3 (certainly step2
should certainly not be included). The first step might change the
behaviour in some very exotic cases, but then the currrent behaviour could
be considered as a bug as well.

I'd be +0.5 to include step 1 in RC3. Any other opinions ?

    Thomas


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


RE: Criteria.setDbName() and BaseXXXPeer.setDbName()

Posted by Thomas Fischer <tf...@apache.org>.

On Sun, 15 Apr 2007, Thomas Fischer wrote:
> On Fri, 13 Apr 2007, Greg Monroe wrote:
>
>> One thing to keep in mind as you're modifying this is
>> the long term request/idea of separating out the Torque
>> "schema" name function that DBName does from the
>> DB connection function.
>> 
>> If I remember right, a big problem in this is that
>> there are a lot of areas where objects are created
>> without access to criteria / connection info.  These
>> are the problem area, since it defaults to the compiled
>> DB connection info.
>
> The idea would be to use Torqie.getDefaultDB() instead of DATABASE_NAME in 
> all places expect where the DatabaseMap is accessed. Not too difficult, one 
> just has to look at every usage of DATABASE_MAP.

At second thought, this is not a valid soution. Now, pople who know they 
have two different databases with two different table sets at generate 
time have the expected behaviour (access the correct database with each 
peer) at runtime. The change outlined above would break this behaviour. 
And I do not see any way of retaining this behaviour without generating 
this information into the Peer classes :-(

    Thomas

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


RE: Criteria.setDbName() and BaseXXXPeer.setDbName()

Posted by Thomas Fischer <tf...@apache.org>.
> I'd suggest at least the following change:
> - if Criteria.setDbName(null) is called, criteria's DbName instance
> variable should set to null, not Torque.getDefaultDb(). Null values are 
> not
> a problem, the instance variable is null anyway if it is not explicitly
> set.
> - in BaseXXXPeer, the check should be changed from if (crit.getDbName() 
> == Torque.getDefaultDB()) to if (crit.getDbName() == null)

This is not as easy as I thought. The problem is that I was mistaken about 
criteria.getDbName() would return null if criteria.setDbName() was not 
used. This is not true, instead criteria.getDbName() returns 
Torque.getDefaultDb().

So if the above change would be made, one would either have to check every 
place where Criteria.getDbName() is used and use Torque.getDefaultDb() if 
Criteria.getDbName() is null. This is about 20 places in Torque's code. 
Alternatively, one could allow null as database name in other places, such 
as Torque.getConnection(String dbName).

Then, Criteria.getDbName() is public, so this method can be called by he 
user. If this is done and the return value is not checked for null, it 
will break the user's code.

So I'd rather not have this change between RC's.

Apropos, this kind of stuff (I'd like to change stuff but not between 
RC's) seems to pile up. We should try to get 3.3 out soon. Anything except 
solutions for the Turbine issue and the "delete with joins" problem needed 
for getting out RC3 ?

     Thomas


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


RE: Criteria.setDbName() and BaseXXXPeer.setDbName()

Posted by Thomas Fischer <tf...@apache.org>.

On Fri, 13 Apr 2007, Greg Monroe wrote:

> One thing to keep in mind as you're modifying this is
> the long term request/idea of separating out the Torque
> "schema" name function that DBName does from the
> DB connection function.
>

That was what I meant by "second step" :-)

> I.e., you should be able to compile the OM once and use
> this with multiple connections. For example, doing
> some sort of copy between DB servers or an app with
> different users accessing their own database area.
>
> Probably beyond the scope of what you're playing with
> but if you see an easy way to do this...

I'd rather not do this between RC's

> If I remember right, a big problem in this is that
> there are a lot of areas where objects are created
> without access to criteria / connection info.  These
> are the problem area, since it defaults to the compiled
> DB connection info.

The idea would be to use Torqie.getDefaultDB() instead of 
DATABASE_NAME in all places expect where the DatabaseMap is accessed. Not 
too difficult, one just has to look at every usage of DATABASE_MAP.

---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


RE: Criteria.setDbName() and BaseXXXPeer.setDbName()

Posted by Greg Monroe <Gr...@DukeCE.com>.
One thing to keep in mind as you're modifying this is
the long term request/idea of separating out the Torque
"schema" name function that DBName does from the 
DB connection function.

I.e., you should be able to compile the OM once and use
this with multiple connections. For example, doing 
some sort of copy between DB servers or an app with 
different users accessing their own database area.

Probably beyond the scope of what you're playing with
but if you see an easy way to do this...

If I remember right, a big problem in this is that
there are a lot of areas where objects are created
without access to criteria / connection info.  These
are the problem area, since it defaults to the compiled
DB connection info.

> -----Original Message-----
> From: Thomas Fischer [mailto:fischer@seitenbau.net] 
> Sent: Friday, April 13, 2007 6:34 AM
> To: Apache Torque Developers List
> Subject: Re: Criteria.setDbName() and BaseXXXPeer.setDbName()
> 
> Thomas Vandahl <th...@tewisoft.de> schrieb am 
> 13.04.2007 12:02:48:
> 
> > Thomas Fischer wrote:
> >
> > > private static void setDbName(Criteria crit) {
> > >     if (crit.getDbName() == Torque.getDefaultDB())
> > >     {
> > >         crit.setDbName(DATABASE_NAME);
> > >     }
> > > }
> > >
> > > So because B (by chance) is the default database, BaseXXXPeer 
> > > decides
> to
> > > replace "B" by its own database, "A", although I have told the 
> > > criteria explicitly to use the database "B".
> >
> Sorry, the example code I used in testing was not the one I 
> used in the email. I used 
> Criteria.setDbName(Torque.getDefaultDb()) and thought I 
> should clarify this by writing "B" instead, but this leads to 
> different results :-(. Thanks for noting.
> But this does not change the point that I explicitly stated 
> in the code that the Default DB should be used and it does 
> not get used.
> 
> > (Generally I agree that this behaviour is highly mystic and 
> should be
> > changed.)
> 
> Do you think it should be changed in RC3 ?
> 
>   Thomas
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
> For additional commands, e-mail: torque-dev-help@db.apache.org
> 
> 

Duke CE Privacy Statement
Please be advised that this e-mail and any files transmitted with it are confidential communication or may otherwise be privileged or confidential and are intended solely for the individual or entity to whom they are addressed.  If you are not the intended recipient you may not rely on the contents of this email or any attachments, and we ask that you  please not read, copy or retransmit this communication, but reply to the sender and destroy the email, its contents, and all copies thereof immediately.  Any unauthorized dissemination, distribution or copying of this communication is strictly prohibited.



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Re: Criteria.setDbName() and BaseXXXPeer.setDbName()

Posted by Thomas Fischer <fi...@seitenbau.net>.
Thomas Vandahl <th...@tewisoft.de> schrieb am 13.04.2007 12:02:48:

> Thomas Fischer wrote:
>
> > private static void setDbName(Criteria crit)
> > {
> >     if (crit.getDbName() == Torque.getDefaultDB())
> >     {
> >         crit.setDbName(DATABASE_NAME);
> >     }
> > }
> >
> > So because B (by chance) is the default database, BaseXXXPeer decides
to
> > replace "B" by its own database, "A", although I have told the criteria
> > explicitly to use the database "B".
>
Sorry, the example code I used in testing was not the one I used in the
email. I used Criteria.setDbName(Torque.getDefaultDb()) and thought I
should clarify this by writing "B" instead, but this leads to different
results :-(. Thanks for noting.
But this does not change the point that I explicitly stated in the code
that the Default DB should be used and it does not get used.

> (Generally I agree that this behaviour is highly mystic and should be
> changed.)

Do you think it should be changed in RC3 ?

  Thomas


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org


Re: Criteria.setDbName() and BaseXXXPeer.setDbName()

Posted by Thomas Vandahl <th...@tewisoft.de>.
Thomas Fischer wrote:

> private static void setDbName(Criteria crit)
> {
>     if (crit.getDbName() == Torque.getDefaultDB())
>     {
>         crit.setDbName(DATABASE_NAME);
>     }
> }
> 
> So because B (by chance) is the default database, BaseXXXPeer decides to
> replace "B" by its own database, "A", although I have told the criteria
> explicitly to use the database "B".

I'm not quite sure why this happens. The expression above clearly 
compares instances, not string values. So the expression should only 
evaluate to true if you set criteria.setDbName(Torque.getDefaultDB()); 
Any other string "B" should evaluate to false, because it is not the 
same string object. Can you confirm this?

(Generally I agree that this behaviour is highly mystic and should be 
changed.)

Bye, Thomas.


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org