users@glassfish.java.net

Re: Problem with strange ClassCastException

From: Piero Filippin <filippinp_at_yahoo.co.uk>
Date: Thu, 06 Sep 2007 16:32:10 +0100

Nice blog, and the tutorial looks great!

I am trying to follow your advice (if I understood correctly), replacing:

EntityManagerFactory emf
=Persistence.createEntityManagerFactory(*"database"*);
em = emf.createEntityManager();

with

@PersistenceContext(*unitName="database"*)
private EntityManager em;

However, now I get a weird error during the deployment:

[#|2007-09-06T16:02:16.492+0100|SEVERE|sun-appserver9.1|javax.enterprise.system.tools.deployment|_ThreadID=14;_ThreadName=Timer-5;_RequestID=fd6d5192-2ad9-492b-b0fc-9967e3ca9f47;|Exception
occured in J2EEC Phase
com.sun.enterprise.deployment.backend.IASDeploymentException: Deployment
Errorjava.sql.SQLException: Error in allocating a connection. Cause:
Connection could not be allocated because: java.net.ConnectException :
Error connecting to server localhost on *port 1527* with message
Connection refused. --
Internal Exception: java.sql.SQLException: Error in allocating a
connection. Cause: Connection could not be allocated because:
java.net.ConnectException : Error connecting to server localhost on port
1527 with message Connection refused.
Error Code: 0
        at
oracle.toplink.essentials.exceptions.DatabaseException.sqlException(DatabaseException.java:305)
        ...

It looks at a first look it is not reading at all the persistance.xml
file (but it is, as if I modify the unitName I get a "pu not found"
error") and tries to connect to something else (oracle db?) instead of
using MySQL.

This is my persistence.xml:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
        <persistence-unit *name="database"*>
                <class> ... </class>
                ...
                <class> ... </class>
                <properties>
                        <property name="toplink.jdbc.driver" value
="com.mysql.jdbc.Driver" />
                        <property name="toplink.jdbc.url"
value="jdbc:mysql://XXX.XXX.XXX.XXX:3306/ejb" />
                        <property name="toplink.jdbc.user" value="ejb" />
                        <property name="toplink.jdbc.password"
value="ejb" />
                        <property name="toplink.logging.level"
value="FINE" />
                        <property name="toplink.ddl-generation"
value="create-tables" />
                </properties>
        </persistence-unit>
</persistence>

Any clue?

Piero

Sahoo wrote:
> Your use case (web -> ejb -> jpa) is explained in this example [1].
> That should get you started without having to read 1300 pages of manual.
>
> Thanks,
> Sahoo
>
> [1]
> http://weblogs.java.net/blog/ss141213/archive/2005/12/using_java_pers.html
>
>
> Piero Filippin wrote:
>> Thank you Sahoo,
>>
>> I am pretty sure #3235 is exactly the problem I am having - I am
>> creating a Persistence.creatEntityManagerfactory() but I never close it.
>> My fault - I copied a piece of code from an example, it worked, so I
>> never read the documentation assuming it was GC, this never created
>> any problem, because I tested the persistance classes directly using
>> the toplink jars (without going through GlassFish) so a new JVM was
>> created for each run, hiding the problem.
>>
>> I will do some testing and post the results, thank you A LOT for your
>> answer.
>>
>> Piero
>>
>> PS: I am trying to convince my boss that JEE is the way to implement
>> a LARGE project we are about to start, I started implementing a
>> smaller project to assess the suitability, but obviously I am a n00b
>> and I still find this technology really hard to digest - to many
>> things to learn at once, and a 1300 pages tutorial does not help!
>>
>> Sahoo wrote:
>>> Is yours similar to symptoms discussed in issue #3235 [1]. Are you
>>> using Persistence.creatEntityManagerfactory() in your code? If so,
>>> when do you close the EMF? Can you switch to using container managed
>>> entity manager (i.e. @PersistenceContext EntityManager em)?
>>>
>>> Thanks,
>>> Sahoo
>>>
>>> [1] https://glassfish.dev.java.net/issues/show_bug.cgi?id=3235
>>>
>>> Piero Filippin wrote:
>>>> Hi,
>>>> I am still a novice with Java EE, so this is probably my fault, but
>>>> I keep having this annoying problem and I cannot find any reference
>>>> to it in the FAQ or in the mailing list.
>>>>
>>>> Basically, I am trying to develop a small application (persistence
>>>> entities->Enterprise bean->web app). I am at the point where the
>>>> persistence entities are (almost) complete, and I am implementing
>>>> the ejb with the business logic.
>>>>
>>>> My problem is: almost everytime I redeploy the updated ejb to
>>>> Glassfish (58b) , when I try to connect to it I get in the
>>>> glassfish log things like
>>>>
>>>> [#|2007-09-06T12:03:49.364+0100|WARNING|sun-appserver9.1|javax.enterprise.system.stream.err|_ThreadID=17;_ThreadName=p:
>>>> thread-pool-1; w:
>>>> 16;_RequestID=399e0846-99c0-499c-a813-72774d30cc2b;|java.lang.ClassCastException:
>>>> *com.mycompany.ejb.User* cannot be cast to *com.mycompany.ejb.User*
>>>>
>>>> Please note this is a problem casting the class to itself, and it
>>>> happens even if the *com.mycompany.ejb.User* class has NOT changed
>>>> (but it is redeployed with the ejb).
>>>>
>>>> To solve the problem I have to restart glassfish, but obviously
>>>> this takes a lot of time (at least one minute between the
>>>> redeployement, bringing down GF, bringing it up again and wait for
>>>> toplink initialisation).
>>>>
>>>> My guess is that the "old" class stays loaded even when the
>>>> out-of-date ejb is automatically undeployed during the
>>>> redeployment, and then the JVM detects the class has changed (at
>>>> least as file timestamp) .
>>>>
>>>> My questions are: am I doing something wrong?
>>>> Is there a trick to avoid this (like adding a serialVersionUID)?
>>>>
>>>> Thank you a lot for any clue.
>>>> Piero
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_glassfish.dev.java.net
>> For additional commands, e-mail: users-help_at_glassfish.dev.java.net
>>
>