Skip to main content

Hibernate and Spring not working on Glassfish 2

11 replies [Last post]
Anonymous

Hello,

I'm trying to port my app from Tomcat 6 to Glassfish 2.
I'm using Spring 2 and Hibernate 3.2
In persistence.xml I set the transaction-type to RESOURCE_LOCAL but
Glassfish complains every time that this can't be used:

com.sun.enterprise.deployment.backend.IASDeploymentException: Deployment
Error -- The persistence-context-ref-name [GenericService/em] in module
[C:\glassfish\domains\domain1\applications\j2ee-modules\bla] resolves to
a persistence unit called [bla] which is of type RESOURCE_LOCAL. Only
persistence units with transaction type JTA can be used as a container
managed entity manager. Please verify your application.

Here is the persistence.xml:
http://holl.co.at/download/glassfish/persistence.txt

And Springs applicationContext.xml:
http://holl.co.at/download/glassfish/applicationContext.txt

Why is it not possible to use RESOURCE_LOCAL?

cheers,
Gerald

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
caroljmcdonald
Offline
Joined: 2003-06-16

With Glassfish V1, I could use Spring with a container managed entity manager :
@PersistenceContext(unitName = "PetCatalogPu")
private EntityManager em;
To do this, I had to configure The Entity Manager factory and the transactionManager in the spring applicationContext.xml .

..


...
this no longer works in Glassfish v2 (I get the same error as Gerald Holl), apparently because spring does not use JTA for this.
I noticed that spring provides a JTA manager for Weblogic and Websphere:
class="org.springframework.transaction.jta.WebLogicJtaTransactionManager">

value="javax.transaction.TransactionManager"/>

Has anyone used Spring 2.x , JPA and Glassfish 2 successfully? Is the solution to only use
Application Managed entity manager?

caroljmcdonald
Offline
Joined: 2003-06-16

my Spring 2.5 , JPA and Glassfish v2 app works now with the transaction manager configured as follows:

I had actually tried this before, but it did NOT work when I had the tranasactionManager properties set as follows:

klausstake
Offline
Joined: 2003-06-12

Hi,

could you please given an example of your persistence.xml and applicationContext.xml
Are you using RESOURCE_LOCAL or JTA?
Are you using JPA?

Thanks

caroljmcdonald
Offline
Joined: 2003-06-16

I'm using JTA, JPA with Toplink as the persistence provider. You can download the complete source code from my blog on this:

http://weblogs.java.net/blog/caroljmcdonald/archive/2008/01/sample_appli...

klausstake
Offline
Joined: 2003-06-12

Thanks for your answer. Unfortunately I am looking for a working solution using JTA, JPA and Hibernte. I am struggling since a few days. I don't get it.

woel
Offline
Joined: 2007-09-06

We're using hibernate with Glassfish. You could either put required hibernate in your gf lib directory or, as we do, together with your application in an ear file. Then just add the following line within the persistence-unit element in your persistence xml file:
org.hibernate.ejb.HibernatePersistence Remember to specifiy jta-data-source and add the datasource in gf.

klausstake
Offline
Joined: 2003-06-12

Actually I've tried what woel suggested.
Unfortunately we had some major issues with this. E.g. I have seen a lot of cases where the Hibernate JPA implementation tries to load the persistence.xml without luck. The reason is that the jars in the gf lib directory are loaded by another classloader than the web application (which acutually contains the persistence.xml). Thus hibernate is not able to find the persistence.xml if you put the hibernate jars in the gf lib dir.
Another issue I have found is some weird behaviour if you are not careful using the right web.xml schema. Glassfish may behave completely different depending on the the schema version you are using. I made some tests using 2.4 and 2.5 and found out that the deployment process completes with totally different results/errors sometimes. E.g. you might not be able to put in RESOURCE_LOCAL if you are using 2.5. Maybe this helps some other people who are in favour of RESOURCE_LOCAL instead of JTA

Sahoo

glassfish@javadesktop.org wrote:
> Actually I've tried what woel suggested.
> Unfortunately we had some major issues with this. E.g. I have seen a lot of cases where the Hibernate JPA implementation tries to load the persistence.xml without luck. The reason is that the jars in the gf lib directory are loaded by another classloader than the web application (which acutually contains the persistence.xml). Thus hibernate is not able to find the persistence.xml if you put the hibernate jars in the gf lib dir.
>
It's not just GlassFish, many appservers use different classloaders to
load system classes vis-a-vis application classes. What is important is
the class loader delegation chain and thread's context class loader. In
GlassFish, an application's class loader delegates to the class loader
that loads stuff from lib directory. We also set the application's class
loader as the current thread's context class loader. Hibernate should be
using thread's context class loader to load persistence.xml. I will be
surprised to know if they actually don't. Which version of Hibernate are
you using where you see such problems? Have you reported in their forum?
> Another issue I have found is some weird behaviour if you are not careful using the right web.xml schema. Glassfish may behave completely different depending on the the schema version you are using. I made some tests using 2.4 and 2.5 and found out that the deployment process completes with totally different results/errors sometimes. E.g. you might not be able to put in RESOURCE_LOCAL if you are using 2.5. Maybe this helps some other people who are in favour of RESOURCE_LOCAL instead of JTA
>
One of the common problems encountered by users is that for them
injection does not happen because they use schema version in web.xml as
2.4. It goes away when they change it to 2.5. Schema version comes into
play because of the Java EE spec requirement; it is *not* a GlassFish
specific behavior. Every compliant application server has to consider
schema version of the descriptors to determine whether to process
annotations or not.

I think the current issue is different. It appears to be a set up issue
or a Sailfin thing. It is now being discussed in sailfin forum.

Thanks,
Sahoo
> [Message sent by forum member 'klausstake' (klausstake)]
>
> http://forums.java.net/jive/thread.jspa?messageID=257601
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

woel
Offline
Joined: 2007-09-06

We've never had any problems with hibernate when we put the libs in the gf lib directory. However, when we moved over to package the hibernate libs with the application we've had some problems from time to time with gf when running it from eclipse. Gf then tells us that it can't find the persistence unit. This might be an eclipse deployment issue. We never have any problems when we deploy the packaged ear file.

Sahoo

Gerald Holl wrote:
> Hello,
>
> I'm trying to port my app from Tomcat 6 to Glassfish 2.
> I'm using Spring 2 and Hibernate 3.2
> In persistence.xml I set the transaction-type to RESOURCE_LOCAL but
> Glassfish complains every time that this can't be used:
>
> com.sun.enterprise.deployment.backend.IASDeploymentException:
> Deployment Error -- The persistence-context-ref-name
> [GenericService/em] in module
> [C:\glassfish\domains\domain1\applications\j2ee-modules\bla] resolves
> to a persistence unit called [bla] which is of type RESOURCE_LOCAL.
> Only persistence units with transaction type JTA can be used as a
> container managed entity manager. Please verify your application.
>
> Here is the persistence.xml:
> http://holl.co.at/download/glassfish/persistence.txt
>
> And Springs applicationContext.xml:
> http://holl.co.at/download/glassfish/applicationContext.txt
>
>
> Why is it not possible to use RESOURCE_LOCAL?
>
The reason is stated in the exception. It as follows:
*only persistence units with transaction type JTA can be used as a
container managed entity manager.*

You may ask what is a container managed entity manager. An entity
manager whose life cycle (i.e., create & close) is controlled by
container is called a container managed life cycle. An injected entity
manager is an example of such type.

Two possible solutions:
1. Change transaction type to JTA in persistence.xml. OR
2. Use Application Managed entity manager (i.e. use EntityManagerFactory
API to create EntityManager).

I don't know which of the two options is more appropriate for you. I
guess it's the former one.

Thanks,
Sahoo
> cheers,
> Gerald
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
> For additional commands, e-mail: users-help@glassfish.dev.java.net
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net

Gerald Holl

Sahoo wrote:
>
> Gerald Holl wrote:
>> Hello,
>>
>> I'm trying to port my app from Tomcat 6 to Glassfish 2.
>> I'm using Spring 2 and Hibernate 3.2
>> In persistence.xml I set the transaction-type to RESOURCE_LOCAL but
>> Glassfish complains every time that this can't be used:
>>
>> com.sun.enterprise.deployment.backend.IASDeploymentException:
>> Deployment Error -- The persistence-context-ref-name
>> [GenericService/em] in module
>> [C:\glassfish\domains\domain1\applications\j2ee-modules\bla] resolves
>> to a persistence unit called [bla] which is of type RESOURCE_LOCAL.
>> Only persistence units with transaction type JTA can be used as a
>> container managed entity manager. Please verify your application.
>>
>> Here is the persistence.xml:
>> http://holl.co.at/download/glassfish/persistence.txt
>>
>> And Springs applicationContext.xml:
>> http://holl.co.at/download/glassfish/applicationContext.txt
>>
>>
>> Why is it not possible to use RESOURCE_LOCAL?
>>
> The reason is stated in the exception. It as follows:
> *only persistence units with transaction type JTA can be used as a
> container managed entity manager.*
>
> You may ask what is a container managed entity manager. An entity
> manager whose life cycle (i.e., create & close) is controlled by
> container is called a container managed life cycle. An injected entity
> manager is an example of such type.
>
> Two possible solutions:
> 1. Change transaction type to JTA in persistence.xml. OR
> 2. Use Application Managed entity manager (i.e. use EntityManagerFactory
> API to create EntityManager).
>
> I don't know which of the two options is more appropriate for you. I
> guess it's the former one.

I switched to EJB completely and removed all the Spring and Hibernate stuff.

Gerald
--
http://holl.co.at

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: users-help@glassfish.dev.java.net