You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openjpa.apache.org by Daryl Stultz <da...@6degrees.com> on 2009/05/21 21:38:22 UTC

Webapp restart problem

Hello, I've been doing various "academic" work in a standalone Java app
getting to know things. Now I am putting together a Tomcat webapp and
running into a problem. From first start, everything is working fine. When
the app reloads, however, I get this:

<openjpa-1.2.0-r422266:683325 nonfatal general error>
org.apache.openjpa.persistence.PersistenceException: null
        at
org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:196)
        at
org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:142)
        at
org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:192)

The original cause is a NPE in my own code. I am using "MyDataSource" to get
connections so I can use my own connection pool and configuration setup. I
have a class (Db) that represents "the database". It has a static member
(HashMap) that holds one or more database "registries".
MyDataSource.getConnection pulls the Db instance from the map using the
configured registry name and then pulls a connection off the registry
(instance of Db). It is this registry map that is null. Again it's a static
member of Db and it is initialized in the field (and never written to
anywhere else. After reload, I can get connections and entity managers
successfully a number of different ways except I can't get an EM using
MyDataSource. This makes me think there is some sort of class loader issue
(mine, or OpenJPA). Is this sort of thing familiar to anyone? Could there be
some sort of static reference to the original instance of MyDataSource that
is causing the call after reload to reference the first set of classes?
MyDataSource IS being reinstantiated after the reload and getConnection is
called on the new instance, but I don't know if there might be something
else weird going on.

Thanks.


-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Two Entities in an inheritance strategy InheritanceStrategy.JOINED -> always uses most specific Entity

Posted by it...@daimler.com.
Hello,

In our database model, we have a relationship between a table called 
'Partner' and a table called 'Vertriebspartner' that specializes the first 
by adding more information to the 'Partner'. However, there is not always 
an entry in the table 'Vertriebspartner' for every 'Partner'.

In my model I currently have the following inheritance relation defined 
for these two tables:

@Entity
@Table(name = "PARTNER", schema = "PART")
public class Partner
{
    @Id
    @Column(name = "PART_KEY")
    private BigDecimal partKey;
}

@Entity
@Table(name = "VERTRIEBSPARTNER")
@Inheritance(strategy = InheritanceType.JOINED)
@AttributeOverride(name="version", column=@Column(name="VERSION")) 
public class Vertriebspartner extends Partner 
{
}

I now have the problem, that in case I use a 'Partner' in a OneToMany or 
ManyToOne-Relationship, openJPA always tries (even though i use Partner 
and NOT Vertriebspartner) to join both. This leads to missing entries, as 
the join often leads to no entry at all, but there is definilty a Partner.

Thus:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "PART_KEY", table = "WBZ_V")
private Partner partner;

ends in a select that joins Partner and Vertriebspartner. I even tried to 
apply targetEntity = Partner.class to the ManyToOne, but that did not help 
at all.

What do I do wrong here? Is the joined inheritance strategy not ment to be 
used when there is not always an entry available in the more specialized 
table? Is this maybe a problem in openJPA?

Thanks,

Heiko

If you are not the intended addressee, please inform us immediately that you have received this e-mail in error, and delete it. We thank you for your cooperation.  

[SOLVED] Re: Re: @DataStoreId + Generator + omitting any @Id-Fields

Posted by it...@daimler.com.
Hey Kevin,

thank you, that clarifies it definitly. My problem now is, that as I'm 
unable to change the schema in the database by adding the missing 
PK-field, I've to
use a native query to retrieve the values. Maybe I can convince the guys 
that maintain the database to rethink their entities, as the entity I need 
to
retrieve is not week, thus the relationship model is not fulfiled at all.

Thanks again for clarifying things,

Heiko

kwsutter@gmail.com schrieb am 22.05.2009 20:23:54:

> Hi Heiko,
> My apologies if my other reply implied that no PK would be required. 
What
> the @DataStoreId provides is the ability to hide an PK field from the 
Entity
> definition.  But, JPA itself (and OpenJPA) requires a PK field of some 
sort
> in order to perform normal Entity operations (insert, find, etc).  Hope 
this
> helps clarify the usage.
> 
> Kevin
> 
> 
> On Fri, May 22, 2009 at 8:24 AM, <it...@daimler.com> wrote:
> 
> > Hello,
> >
> > as I was reading the thread 'Can we have entities without @Id' 
carefully,
> > I read about the possibility to use @DataStoreId to modify the way a
> > primary key column can be retrieved. However, I do NOT understand
> > correctly how to do this.
> >
> > I have an issue where I'm not able to change the table-model within 
the
> > database, but have to create entities from a table that does not 
provide a
> > primary key at all. As it is simply necessary to read information from
> > this table I wanted to use DataStoreId to let JPA generate an Id 
Column
> > itself and add it to the entity.
> >
> > However, this does not seem to work as I expected. I added the 
following
> > code:
> >
> > @Entity
> > @Table(name = "WBZ")
> > @DataStoreId(strategy = GenerationType.SEQUENCE, generator="time")
> > public class WirtschaftlicheBeziehung
> > {
> >        // no id fields in here
> > }
> >
> > As far as I can see in the openJPA source, this leads to an automatic
> > generation of an 'id' column. My problem now is, I do not have an 'id'
> > column in the database, thus this crashes with an exception, that the
> > table WBZ lacks a column named 'id'.
> >
> > My question now is, is there a way to use DataStoreId to handle tables
> > without primary key columns? I do not fully understand the usage of 
the
> > special annotation of DataStoreId when it still searches for a column 
in
> > the database. I thought I do not have to have such a column?
> >
> > Thanks for your time!
> >
> > Heiko
> >
> > If you are not the intended addressee, please inform us immediately 
that
> > you have received this e-mail in error, and delete it. We thank you 
for your
> > cooperation.


If you are not the intended addressee, please inform us immediately that you have received this e-mail in error, and delete it. We thank you for your cooperation.  

Re: @DataStoreId + Generator + omitting any @Id-Fields

Posted by Kevin Sutter <kw...@gmail.com>.
Hi Heiko,
My apologies if my other reply implied that no PK would be required.  What
the @DataStoreId provides is the ability to hide an PK field from the Entity
definition.  But, JPA itself (and OpenJPA) requires a PK field of some sort
in order to perform normal Entity operations (insert, find, etc).  Hope this
helps clarify the usage.

Kevin


On Fri, May 22, 2009 at 8:24 AM, <it...@daimler.com> wrote:

> Hello,
>
> as I was reading the thread 'Can we have entities without @Id' carefully,
> I read about the possibility to use @DataStoreId to modify the way a
> primary key column can be retrieved. However, I do NOT understand
> correctly how to do this.
>
> I have an issue where I'm not able to change the table-model within the
> database, but have to create entities from a table that does not provide a
> primary key at all. As it is simply necessary to read information from
> this table I wanted to use DataStoreId to let JPA generate an Id Column
> itself and add it to the entity.
>
> However, this does not seem to work as I expected. I added the following
> code:
>
> @Entity
> @Table(name = "WBZ")
> @DataStoreId(strategy = GenerationType.SEQUENCE, generator="time")
> public class WirtschaftlicheBeziehung
> {
>        // no id fields in here
> }
>
> As far as I can see in the openJPA source, this leads to an automatic
> generation of an 'id' column. My problem now is, I do not have an 'id'
> column in the database, thus this crashes with an exception, that the
> table WBZ lacks a column named 'id'.
>
> My question now is, is there a way to use DataStoreId to handle tables
> without primary key columns? I do not fully understand the usage of the
> special annotation of DataStoreId when it still searches for a column in
> the database. I thought I do not have to have such a column?
>
> Thanks for your time!
>
> Heiko
>
> If you are not the intended addressee, please inform us immediately that
> you have received this e-mail in error, and delete it. We thank you for your
> cooperation.

Re: Webapp restart problem

Posted by Daryl Stultz <da...@6degrees.com>.
2009/5/22 Kevin Sutter <kw...@gmail.com>

> Does anybody know if Tomcat defines
> how to install and configure JPA providers?


It would be nice if the makers or various libraries/frameworks, etc. stated
where the jars should be put. Of course, in this case it was my code that
required it in WEB-INF, but while some can go in CATALINA/lib depending on
how you use them, some can't or really shouldn't. It's hard to figure that
out, sometimes. I prefer to put jars in CATALINA/lib so as to save PERM GEN
memory space upon reload. Anyway, whole other matter.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Webapp restart problem

Posted by Kevin Sutter <kw...@gmail.com>.
So, this looks to be a Classloader issue with Tomcat.  As you have found
out, putting the OpenJPA provider jars in your application will get around
this issue.  But, it's still an issue.  Does anybody know if Tomcat defines
how to install and configure JPA providers?  I know that WebSphere has
defined steps for supporting various JPA providers [1].  I wonder if Tomcat
has something similar?

Kevin

[1]
http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/topic/com.ibm.websphere.nd.multiplatform.doc/info/ae/ae/tejb_configpp.html

2009/5/22 Daryl Stultz <da...@6degrees.com>

> On Fri, May 22, 2009 at 8:18 AM, Miłosz Tylenda <mt...@o2.pl> wrote:
>
> > That rings me on Tomcat clearing static variables [1] after reload, see
> > org.apache.catalina.loader. WebappClassLoader.ENABLE_CLEAR_REFERENCES
> > property. Maybe this explains something.
> >
>
> That does in fact change the behavior but only reinforces my suspicion that
> after the redeploy, there are references to the static member from the
> first
> start. Here are some print statements with ENABLE_CLEAR_REFERENCES=false:
> *
> (See note at bottom before wasting your time reading next block)*
>
> FIRST START
> Created new EM Factory
> org.apache.openjpa.persistence.EntityManagerFactoryImpl@3f7ede
> Instantiated JpaDataSource com.sixdegreessoftware.sql.JpaDataSource@1a1cf5
> JpaDataSource.getConnection()
> com.sixdegreessoftware.sql.JpaDataSource@1a1cf5
> dbRegistryMap = {default=com.sixdegreessoftware.sql.DbJdbc@e05748}
> db = com.sixdegreessoftware.sql.DbJdbc@e05748
>
> REDEPLOY
>
> Created new EM Factory
> org.apache.openjpa.persistence.EntityManagerFactoryImpl@ca1676
> Instantiated JpaDataSource com.sixdegreessoftware.sql.JpaDataSource@ee411c
> JpaDataSource.getConnection()
> com.sixdegreessoftware.sql.JpaDataSource@ee411c
> dbRegistryMap = {default=com.sixdegreessoftware.sql.DbJdbc@e05748}
> db = com.sixdegreessoftware.sql.DbJdbc@e05748
>
> Note after redeploy the JpaDataSource instance has changed but
> dbRegistryMap
> and db are the instances from the FIRST START. dbRegistryMap is the static
> member coming up null when ENABLE_CLEAR_REFERENCES=true (the default). It
> should be null, really, it's a member of a class that got reloaded and
> should not be referenced anymore.
>
> Maybe someone can try this with ENABLE_CLEAR_REFERENCES=true (the default).
> If you have a Tomcat application with OpenJPA and a DataSource, subclass
> your DataSource with this:
>
> import java.io.PrintWriter;
> import java.sql.*;
> import java.sql.Connection;
>
> import javax.sql.DataSource;
>
> public class MyDataSource extends YourExistingDataSource {
>
>    private static String testValue = "Shazaam!";
>
>    public MyDataSource() {
>        super();
>        System.out.println("Instantiated MyDataSource " + this);
>    }
>
>    public Connection getConnection() throws SQLException {
>        System.out.println("MyDataSource.getConnection() " + this + "
> testValue " + testValue);
>        return super.getConnection();
>    }
>
> }
>
> Note that Shazaam! is printed in the first start phase but null after
> redeploy.
>
> *Hey WAAAAAIIIIIIIT a minute! *The class with the member coming up null is
> in my webapp/WEB-INF/lib folder. openjpa.jar and geronimo.jar (et al) are
> in
> catalina/lib. If I move openjpa jars to webapp/WEB-INF/lib the problem goes
> away. DANG! Sorry to bother everyone.
>
> --
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
>

Re: Webapp restart problem

Posted by Daryl Stultz <da...@6degrees.com>.
On Fri, May 22, 2009 at 8:18 AM, Miłosz Tylenda <mt...@o2.pl> wrote:

> That rings me on Tomcat clearing static variables [1] after reload, see
> org.apache.catalina.loader. WebappClassLoader.ENABLE_CLEAR_REFERENCES
> property. Maybe this explains something.
>

That does in fact change the behavior but only reinforces my suspicion that
after the redeploy, there are references to the static member from the first
start. Here are some print statements with ENABLE_CLEAR_REFERENCES=false:
*
(See note at bottom before wasting your time reading next block)*

FIRST START
Created new EM Factory
org.apache.openjpa.persistence.EntityManagerFactoryImpl@3f7ede
Instantiated JpaDataSource com.sixdegreessoftware.sql.JpaDataSource@1a1cf5
JpaDataSource.getConnection()
com.sixdegreessoftware.sql.JpaDataSource@1a1cf5
dbRegistryMap = {default=com.sixdegreessoftware.sql.DbJdbc@e05748}
db = com.sixdegreessoftware.sql.DbJdbc@e05748

REDEPLOY

Created new EM Factory
org.apache.openjpa.persistence.EntityManagerFactoryImpl@ca1676
Instantiated JpaDataSource com.sixdegreessoftware.sql.JpaDataSource@ee411c
JpaDataSource.getConnection()
com.sixdegreessoftware.sql.JpaDataSource@ee411c
dbRegistryMap = {default=com.sixdegreessoftware.sql.DbJdbc@e05748}
db = com.sixdegreessoftware.sql.DbJdbc@e05748

Note after redeploy the JpaDataSource instance has changed but dbRegistryMap
and db are the instances from the FIRST START. dbRegistryMap is the static
member coming up null when ENABLE_CLEAR_REFERENCES=true (the default). It
should be null, really, it's a member of a class that got reloaded and
should not be referenced anymore.

Maybe someone can try this with ENABLE_CLEAR_REFERENCES=true (the default).
If you have a Tomcat application with OpenJPA and a DataSource, subclass
your DataSource with this:

import java.io.PrintWriter;
import java.sql.*;
import java.sql.Connection;

import javax.sql.DataSource;

public class MyDataSource extends YourExistingDataSource {

    private static String testValue = "Shazaam!";

    public MyDataSource() {
        super();
        System.out.println("Instantiated MyDataSource " + this);
    }

    public Connection getConnection() throws SQLException {
        System.out.println("MyDataSource.getConnection() " + this + "
testValue " + testValue);
        return super.getConnection();
    }

}

Note that Shazaam! is printed in the first start phase but null after
redeploy.

*Hey WAAAAAIIIIIIIT a minute! *The class with the member coming up null is
in my webapp/WEB-INF/lib folder. openjpa.jar and geronimo.jar (et al) are in
catalina/lib. If I move openjpa jars to webapp/WEB-INF/lib the problem goes
away. DANG! Sorry to bother everyone.

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

@DataStoreId + Generator + omitting any @Id-Fields

Posted by it...@daimler.com.
Hello,

as I was reading the thread 'Can we have entities without @Id' carefully, 
I read about the possibility to use @DataStoreId to modify the way a 
primary key column can be retrieved. However, I do NOT understand 
correctly how to do this. 

I have an issue where I'm not able to change the table-model within the 
database, but have to create entities from a table that does not provide a 
primary key at all. As it is simply necessary to read information from 
this table I wanted to use DataStoreId to let JPA generate an Id Column 
itself and add it to the entity.

However, this does not seem to work as I expected. I added the following 
code:

@Entity
@Table(name = "WBZ")
@DataStoreId(strategy = GenerationType.SEQUENCE, generator="time")
public class WirtschaftlicheBeziehung 
{
        // no id fields in here
}

As far as I can see in the openJPA source, this leads to an automatic 
generation of an 'id' column. My problem now is, I do not have an 'id' 
column in the database, thus this crashes with an exception, that the 
table WBZ lacks a column named 'id'. 

My question now is, is there a way to use DataStoreId to handle tables 
without primary key columns? I do not fully understand the usage of the 
special annotation of DataStoreId when it still searches for a column in 
the database. I thought I do not have to have such a column?

Thanks for your time!

Heiko

If you are not the intended addressee, please inform us immediately that you have received this e-mail in error, and delete it. We thank you for your cooperation.  

Re: Webapp restart problem

Posted by Miłosz Tylenda <mt...@o2.pl>.
> On Thu, May 21, 2009 at 8:57 PM, Pinaki Poddar  wrote:
> 
> >
> > Hi,
> >  If OpenJPA has understood what you told in persistence.xml, then OpenJPA
> > should call
> >    MyDataSource.getConnection()
> >  when a connection is needed.
> >  To quickly verify call:
> >    OpenJPAEntityManager.getConnection();
> >
> > Yes, I have confirmed that both the original instance (first start of
> Tomcat) and the second instance (app reload) of MyDataSource are getting
> getConnection called. It appears that the second instance is then
> referencing static classes from the first start. It's the only thing I can
> think of given that the field that is coming up null is static, instantiated
> in the inititialization and never written to (IOW, how can it possibly be
> null?). Is there any other reasonable cause for such a state?

Hi Daryl!

That rings me on Tomcat clearing static variables [1] after reload, see org.apache.catalina.loader. WebappClassLoader.ENABLE_CLEAR_REFERENCES property. Maybe this explains something.

Cheers,
Milosz

[1] http://tomcat.apache.org/tomcat-6.0-doc/config/systemprops.html



Re: Webapp restart problem

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, May 21, 2009 at 8:57 PM, Pinaki Poddar <pp...@apache.org> wrote:

>
> Hi,
>  If OpenJPA has understood what you told in persistence.xml, then OpenJPA
> should call
>    MyDataSource.getConnection()
>  when a connection is needed.
>  To quickly verify call:
>    OpenJPAEntityManager.getConnection();
>
> Yes, I have confirmed that both the original instance (first start of
Tomcat) and the second instance (app reload) of MyDataSource are getting
getConnection called. It appears that the second instance is then
referencing static classes from the first start. It's the only thing I can
think of given that the field that is coming up null is static, instantiated
in the inititialization and never written to (IOW, how can it possibly be
null?). Is there any other reasonable cause for such a state?

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Webapp restart problem

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  If OpenJPA has understood what you told in persistence.xml, then OpenJPA
should call
    MyDataSource.getConnection() 
  when a connection is needed.  
  To quickly verify call:
    OpenJPAEntityManager.getConnection();



Daryl Stultz wrote:
> 
> On Thu, May 21, 2009 at 5:16 PM, Pinaki Poddar <pp...@apache.org> wrote:
> 
>>
>> Hi,
>>  How did you tell OpenJPA to use "MyDataSource"?
> 
> 
> First I asked nicely, then I got ignorant with it, then I put this in
> persistence.xml:
> 
> <property name="openjpa.ConnectionDriverName"
> value="yadda.yadda.MyDataSource" />
> <property name="openjpa.ConnectionProperties"
> value="DefaultDbRegistryName=default" />
> 
> -- 
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
> 
> 


-----
Pinaki Poddar                      http://ppoddar.blogspot.com/
                                      
http://www.linkedin.com/in/pinakipoddar
OpenJPA PMC Member/Committer
JPA Expert Group Member
-- 
View this message in context: http://n2.nabble.com/Webapp-restart-problem-tp2953817p2955018.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.


Re: Webapp restart problem

Posted by Daryl Stultz <da...@6degrees.com>.
On Thu, May 21, 2009 at 5:16 PM, Pinaki Poddar <pp...@apache.org> wrote:

>
> Hi,
>  How did you tell OpenJPA to use "MyDataSource"?


First I asked nicely, then I got ignorant with it, then I put this in
persistence.xml:

<property name="openjpa.ConnectionDriverName"
value="yadda.yadda.MyDataSource" />
<property name="openjpa.ConnectionProperties"
value="DefaultDbRegistryName=default" />

-- 
Daryl Stultz
_____________________________________
6 Degrees Software and Consulting, Inc.
http://www.6degrees.com
mailto:daryl@6degrees.com

Re: Webapp restart problem

Posted by Pinaki Poddar <pp...@apache.org>.
Hi,
  How did you tell OpenJPA to use "MyDataSource"?

 


Daryl Stultz wrote:
> 
> Hello, I've been doing various "academic" work in a standalone Java app
> getting to know things. Now I am putting together a Tomcat webapp and
> running into a problem. From first start, everything is working fine. When
> the app reloads, however, I get this:
> 
> <openjpa-1.2.0-r422266:683325 nonfatal general error>
> org.apache.openjpa.persistence.PersistenceException: null
>         at
> org.apache.openjpa.kernel.AbstractBrokerFactory.newBroker(AbstractBrokerFactory.java:196)
>         at
> org.apache.openjpa.kernel.DelegatingBrokerFactory.newBroker(DelegatingBrokerFactory.java:142)
>         at
> org.apache.openjpa.persistence.EntityManagerFactoryImpl.createEntityManager(EntityManagerFactoryImpl.java:192)
> 
> The original cause is a NPE in my own code. I am using "MyDataSource" to
> get
> connections so I can use my own connection pool and configuration setup. I
> have a class (Db) that represents "the database". It has a static member
> (HashMap) that holds one or more database "registries".
> MyDataSource.getConnection pulls the Db instance from the map using the
> configured registry name and then pulls a connection off the registry
> (instance of Db). It is this registry map that is null. Again it's a
> static
> member of Db and it is initialized in the field (and never written to
> anywhere else. After reload, I can get connections and entity managers
> successfully a number of different ways except I can't get an EM using
> MyDataSource. This makes me think there is some sort of class loader issue
> (mine, or OpenJPA). Is this sort of thing familiar to anyone? Could there
> be
> some sort of static reference to the original instance of MyDataSource
> that
> is causing the call after reload to reference the first set of classes?
> MyDataSource IS being reinstantiated after the reload and getConnection is
> called on the new instance, but I don't know if there might be something
> else weird going on.
> 
> Thanks.
> 
> 
> -- 
> Daryl Stultz
> _____________________________________
> 6 Degrees Software and Consulting, Inc.
> http://www.6degrees.com
> mailto:daryl@6degrees.com
> 
> 


-----
Pinaki Poddar                      http://ppoddar.blogspot.com/
                                      
http://www.linkedin.com/in/pinakipoddar
OpenJPA PMC Member/Committer
JPA Expert Group Member
-- 
View this message in context: http://n2.nabble.com/Webapp-restart-problem-tp2953817p2954244.html
Sent from the OpenJPA Users mailing list archive at Nabble.com.