You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Adam Jenkins <ma...@adamjenkins.net> on 2005/07/02 01:50:13 UTC

1.1 best practice question

>>From 1.1 onwards, should all access be through
org.apache.ojb.broker.OJB? (i.e. instead of using QueryFactory,
PersistentBrokerFactory etc)


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


Re: 1.1 best practice question - bug?

Posted by Thomas Dudziak <to...@gmail.com>.
On 7/3/05, Adam Jenkins <ma...@adamjenkins.net> wrote:
> No worries...will do it tonight (work becons).  Probably my config, but
> I'll get you more info.
> 
> If it's any help I debugged it to the line
> subContainer.setSingletonInstance(pcd.getJdbcConnectionDescriptor()); in
> PersistenceConfiguration constructor...pcd.getJdbcConnectionDescriptor()
> was returning null.

Can it be that you're requiring the jcd to be the default connection ?
My example did not provide that; for this to work you still have to
register the key at the metadata manager using the setDefaultPBKey()
method.

Tom

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


Re: 1.1 best practice question - bug?

Posted by Thomas Dudziak <to...@gmail.com>.
On 7/3/05, Adam Jenkins <ma...@adamjenkins.net> wrote:

> Thanks for that example.  I changed my code to almost exactly what you
> have below and, while I got my unit tests to pass, I did experience some
> odd behaviour.  The example you gave only seems to work if I have a
> jdbc-connection-descriptor declared in repository.xml with the same
> alias as the PBKey.  If I ommit the jdbc-connection-descriptor or give
> it a different name, I get the npe discussed in previous posts (pasted
> below for convenience).  This is reproducable and was using the latest
> from cvs.  Just thought I'd let you know in case it's a bug.  Let me
> know if you need more information.

That's strange, the very code that I posted works nicely for me with a
repository.xml containing only class-descriptors (current 1.1 CVS).
Could you perhaps build a debug version and repost the stacktrace with
line numbers ?

Tom

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


Re: 1.1 best practice question - bug?

Posted by Adam Jenkins <ma...@adamjenkins.net>.
Hi Tom,

Thanks for that example.  I changed my code to almost exactly what you
have below and, while I got my unit tests to pass, I did experience some
odd behaviour.  The example you gave only seems to work if I have a
jdbc-connection-descriptor declared in repository.xml with the same
alias as the PBKey.  If I ommit the jdbc-connection-descriptor or give
it a different name, I get the npe discussed in previous posts (pasted
below for convenience).  This is reproducable and was using the latest
from cvs.  Just thought I'd let you know in case it's a bug.  Let me
know if you need more information.

Cheers
Adam

---paste---


null
java.lang.NullPointerException
        at
org.apache.ojb.broker.core.configuration.ComponentContainerBase.getAllTypes(Unknown Source)
        at
org.apache.ojb.broker.core.configuration.ComponentContainerBase.setSingletonInstance(Unknown Source)
        at org.apache.ojb.broker.PersistenceConfiguration.<init>(Unknown
Source)
        at
org.apache.ojb.broker.OJB.createPersistenceConfiguration(Unknown Source)
        at org.apache.ojb.broker.OJB.getConfiguration(Unknown Source)
        at org.apache.ojb.broker.OJB.lookupBroker(Unknown Source)

---paste---
On Sun, 2005-07-03 at 12:23 +0200, Thomas Dudziak wrote:
> As I said, you should use the OJB class as an object. Something like
> this should work:
> 
>         OJB                      ojb = new OJB();
>         PBKey                    key = new PBKey("default");
>         JdbcConnectionDescriptor jcd = new JdbcConnectionDescriptor();
> 
>         jcd.setDefaultConnection(false);
>         jcd.setJcdAlias(key.getAlias());
>         jcd.setDbms("postgresql");
>         jcd.setJdbcLevel(3.0);
>         jcd.setDriver("org.postgresql.Driver");
>         jcd.setProtocol("jdbc");
>         jcd.setSubProtocol("postgresql");
>         jcd.setDbAlias("test");
> 
>         DescriptorRepository               model  =
> ojb.getMetadataManager().readDescriptorRepository(new
> FileInputStream("repository.xml"));
>         PersistenceConfigurationDescriptor newPCD = new
> PersistenceConfigurationDescriptor(key, model, jcd);
> 
>         ojb.getMetadataManager().addPersistenceConfiguration(newPCD);
> 
>         PersistenceConfiguration conf   = ojb.getConfiguration(key);
>         PersistenceBroker        broker = conf.createPersistenceBroker();
> 
> 
> Note that it is no longer necessary to store the jcd manually in the
> connectionRepository because this is only maintained for backwards
> compatibility.
> 
> Tom
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 


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


Re: 1.1 best practice question - bug?

Posted by Adam Jenkins <ma...@adamjenkins.net>.
Hi Tom,

Thanks for that example.  I changed my code to almost exactly what you
have below and, while I got my unit tests to pass, I did experience some
odd behaviour.  The example you gave only seems to work if I have a
jdbc-connection-descriptor declared in repository.xml with the same
alias as the PBKey.  If I ommit the jdbc-connection-descriptor or give
it a different name, I get the npe discussed in previous posts (pasted
below for convenience).  This is reproducable and was using the latest
from cvs.  Just thought I'd let you know in case it's a bug.  Let me
know if you need more information.

Cheers
Adam

---paste---


null
java.lang.NullPointerException
        at
org.apache.ojb.broker.core.configuration.ComponentContainerBase.getAllTypes(Unknown Source)
        at
org.apache.ojb.broker.core.configuration.ComponentContainerBase.setSingletonInstance(Unknown Source)
        at org.apache.ojb.broker.PersistenceConfiguration.<init>(Unknown
Source)
        at
org.apache.ojb.broker.OJB.createPersistenceConfiguration(Unknown Source)
        at org.apache.ojb.broker.OJB.getConfiguration(Unknown Source)
        at org.apache.ojb.broker.OJB.lookupBroker(Unknown Source)

---paste---
On Sun, 2005-07-03 at 12:23 +0200, Thomas Dudziak wrote:
> As I said, you should use the OJB class as an object. Something like
> this should work:
> 
>         OJB                      ojb = new OJB();
>         PBKey                    key = new PBKey("default");
>         JdbcConnectionDescriptor jcd = new JdbcConnectionDescriptor();
> 
>         jcd.setDefaultConnection(false);
>         jcd.setJcdAlias(key.getAlias());
>         jcd.setDbms("postgresql");
>         jcd.setJdbcLevel(3.0);
>         jcd.setDriver("org.postgresql.Driver");
>         jcd.setProtocol("jdbc");
>         jcd.setSubProtocol("postgresql");
>         jcd.setDbAlias("test");
> 
>         DescriptorRepository               model  =
> ojb.getMetadataManager().readDescriptorRepository(new
> FileInputStream("repository.xml"));
>         PersistenceConfigurationDescriptor newPCD = new
> PersistenceConfigurationDescriptor(key, model, jcd);
> 
>         ojb.getMetadataManager().addPersistenceConfiguration(newPCD);
> 
>         PersistenceConfiguration conf   = ojb.getConfiguration(key);
>         PersistenceBroker        broker = conf.createPersistenceBroker();
> 
> 
> Note that it is no longer necessary to store the jcd manually in the
> connectionRepository because this is only maintained for backwards
> compatibility.
> 
> Tom
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 


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


Re: 1.1 best practice question

Posted by Thomas Dudziak <to...@gmail.com>.
As I said, you should use the OJB class as an object. Something like
this should work:

        OJB                      ojb = new OJB();
        PBKey                    key = new PBKey("default");
        JdbcConnectionDescriptor jcd = new JdbcConnectionDescriptor();

        jcd.setDefaultConnection(false);
        jcd.setJcdAlias(key.getAlias());
        jcd.setDbms("postgresql");
        jcd.setJdbcLevel(3.0);
        jcd.setDriver("org.postgresql.Driver");
        jcd.setProtocol("jdbc");
        jcd.setSubProtocol("postgresql");
        jcd.setDbAlias("test");

        DescriptorRepository               model  =
ojb.getMetadataManager().readDescriptorRepository(new
FileInputStream("repository.xml"));
        PersistenceConfigurationDescriptor newPCD = new
PersistenceConfigurationDescriptor(key, model, jcd);

        ojb.getMetadataManager().addPersistenceConfiguration(newPCD);

        PersistenceConfiguration conf   = ojb.getConfiguration(key);
        PersistenceBroker        broker = conf.createPersistenceBroker();


Note that it is no longer necessary to store the jcd manually in the
connectionRepository because this is only maintained for backwards
compatibility.

Tom

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


Re: 1.1 best practice question

Posted by Adam Jenkins <ma...@adamjenkins.net>.
I've tracked the error down to:

    public PersistenceConfiguration(ComponentContainer container,
PersistenceConfigurationDescriptor pcd)
    {
        this.pcd = pcd;
        this.ojb = (OJB) container.getInstance(OJB.class);

        subContainer = container.createChildContainer();
        subContainer.setSingletonInstance(this);

subContainer.setSingletonInstance(pcd.getJdbcConnectionDescriptor());



the call to pcd.getJdbcConnectionDescriptor() is returning null...I must
be configuring things wrong...any suggestions?  If this is a bug, let me
know hov you want it fixed and I'll submit a patch.

On Sat, 2005-07-02 at 15:54 +0200, Thomas Dudziak wrote:
> On 7/2/05, Adam Jenkins <ma...@adamjenkins.net> wrote:
> > Sorry, meant to reply to list for posterity...hit wrong button...repost
> > below:
> > 
> > Thanks.  So how do we swap JdbcConnectionDescriptors now?  I'm used to
> > dealing with metadata manager (I have a completely new descriptor
> > pointing to a new database that I want to configure at runtime)
> 
> In 1.1, we introduced with the PersistenceConfiguration a new
> indirection level that allows to configure JCDs and models more easily
> (though that part is not finished yet). In your case, you should
> create a new PersistenceConfigurationDescriptor and add it to the
> metadata manager via the addPersistenceConfiguration method. Then you
> can access it via the PBKey as before.
> 
> Tom
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 


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


Re: 1.1 best practice question

Posted by Adam Jenkins <ma...@adamjenkins.net>.
So...should the following code work?  I keep getting null pointers
whenever I try to set the descriptors myself (regardless of whether I
use the commented or uncommented version below):

---code---
        //final JdbcConnectionDescriptor descriptor =
MetadataManager.getInstance().readConnectionRepository(
        //        getClass().getResourceAsStream("/connections.xml")
        //).getDescriptor(new PBKey("$ALIAS$"));

        final JdbcConnectionDescriptor descriptor = new
JdbcConnectionDescriptor();
        logDescriptor(descriptor);
        descriptor.setDefaultConnection(false);
        descriptor.setJcdAlias(DBKEY.getAlias());
        descriptor.setDbms(configuration.getDBConnect().getPlatform());
        descriptor.setJdbcLevel(getJDBCLevel());
        descriptor.setDriver(configuration.getDBConnect().getDriver());

descriptor.setProtocol(configuration.getDBConnect().getProtocol());

descriptor.setSubProtocol(configuration.getDBConnect().getSubprotocol());

descriptor.setDbAlias(configuration.getDBConnect().getDbalias());

descriptor.setUserName(configuration.getDBConnect().getUsername());

descriptor.setPassWord(configuration.getDBConnect().getPassword());

MetadataManager.getInstance().connectionRepository().addDescriptor(descriptor);
        logDescriptor(descriptor);
        PersistenceConfigurationDescriptor newPCD = new
PersistenceConfigurationDescriptor(
                DBKEY,

MetadataManager.getInstance().readDescriptorRepository(getClass().getResourceAsStream("/model.xml")),
                descriptor
        );

MetadataManager.getInstance().addPersistenceConfiguration(newPCD);



---error---
null
java.lang.NullPointerException
        at
org.apache.ojb.broker.core.configuration.ComponentContainerBase.getAllTypes(Unknown Source)
        at
org.apache.ojb.broker.core.configuration.ComponentContainerBase.setSingletonInstance(Unknown Source)
        at org.apache.ojb.broker.PersistenceConfiguration.<init>(Unknown
Source)
        at
org.apache.ojb.broker.OJB.createPersistenceConfiguration(Unknown Source)
        at org.apache.ojb.broker.OJB.getConfiguration(Unknown Source)
        at org.apache.ojb.broker.OJB.lookupBroker(Unknown Source)


On Sat, 2005-07-02 at 15:54 +0200, Thomas Dudziak wrote:
> On 7/2/05, Adam Jenkins <ma...@adamjenkins.net> wrote:
> > Sorry, meant to reply to list for posterity...hit wrong button...repost
> > below:
> > 
> > Thanks.  So how do we swap JdbcConnectionDescriptors now?  I'm used to
> > dealing with metadata manager (I have a completely new descriptor
> > pointing to a new database that I want to configure at runtime)
> 
> In 1.1, we introduced with the PersistenceConfiguration a new
> indirection level that allows to configure JCDs and models more easily
> (though that part is not finished yet). In your case, you should
> create a new PersistenceConfigurationDescriptor and add it to the
> metadata manager via the addPersistenceConfiguration method. Then you
> can access it via the PBKey as before.
> 
> Tom
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 


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


Re: 1.1 best practice question

Posted by Thomas Dudziak <to...@gmail.com>.
On 7/2/05, Adam Jenkins <ma...@adamjenkins.net> wrote:
> Sorry, meant to reply to list for posterity...hit wrong button...repost
> below:
> 
> Thanks.  So how do we swap JdbcConnectionDescriptors now?  I'm used to
> dealing with metadata manager (I have a completely new descriptor
> pointing to a new database that I want to configure at runtime)

In 1.1, we introduced with the PersistenceConfiguration a new
indirection level that allows to configure JCDs and models more easily
(though that part is not finished yet). In your case, you should
create a new PersistenceConfigurationDescriptor and add it to the
metadata manager via the addPersistenceConfiguration method. Then you
can access it via the PBKey as before.

Tom

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


Re: 1.1 best practice question

Posted by Adam Jenkins <ma...@adamjenkins.net>.
Sorry, meant to reply to list for posterity...hit wrong button...repost
below:

Thanks.  So how do we swap JdbcConnectionDescriptors now?  I'm used to
dealing with metadata manager (I have a completely new descriptor
pointing to a new database that I want to configure at runtime)

Cheers
Adam


On Sat, 2005-07-02 at 10:18 +0200, Thomas Dudziak wrote:
> On 7/2/05, Adam Jenkins <ma...@adamjenkins.net> wrote:
> > >From 1.1 onwards, should all access be through
> > org.apache.ojb.broker.OJB? (i.e. instead of using QueryFactory,
> > PersistentBrokerFactory etc)
> 
> Yes, that's the idea. OJB 1.1 is meant to be completely instance-based
> (as opposed to 1.0 where static parts are used). So in short, you
> should create an OJB object and store it away somewhere, and use this
> object whenever you want to access the database.
> To make migration easier, we maintained the PersistentBrokerFactory,
> though it is likely to become deprecated in the final 1.1.
> 
> Tom
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 


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


Re: 1.1 best practice question

Posted by Thomas Dudziak <to...@gmail.com>.
On 7/2/05, Adam Jenkins <ma...@adamjenkins.net> wrote:
> >From 1.1 onwards, should all access be through
> org.apache.ojb.broker.OJB? (i.e. instead of using QueryFactory,
> PersistentBrokerFactory etc)

Yes, that's the idea. OJB 1.1 is meant to be completely instance-based
(as opposed to 1.0 where static parts are used). So in short, you
should create an OJB object and store it away somewhere, and use this
object whenever you want to access the database.
To make migration easier, we maintained the PersistentBrokerFactory,
though it is likely to become deprecated in the final 1.1.

Tom

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