I have a web application that runs on glassfish 3.1. I am using JPA (Hibernate) configured with spring.
This is my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="myPU">
<class>org.myCompany.entities.Class1</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect" />
<property name="hibernate.show_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
This is part of my ApplicationContext.xml:
<tx:annotation-driven />
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="jdbc/myDataSource" />
</bean>
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="persistenceUnitName" value="myPU" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="databasePlatform" value="org.hibernate.dialect.MySQL5Dialect" />
</bean>
</property>
</bean>
In addition i have declared my datasource in the glassfish server (jdbc connection pool and recourse)
and the JNDI name is "jdbc/myDataSource". The issue is that on my local installation of glassfish, published via the glassfish plugin for eclipse, it works fine BUT when i deploy the war file manually on an installation of glassfish (i tried on my VPS - CentOS) then i get this error:
Error occurred during deployment: Exception while preparing the app : Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: java.sql.SQLException: Connection could not be allocated because: java.net.ConnectException : Error connecting to server localhost on port 1527 with message Connection refused. Error Code: 0. Please see server.log for more details.
For some reason, the application is using the default derby datasource of glassfish and not my defined datasource. anyone knows why and how to overcome this?
Did you configure your JDBC Resource to use the right Connection Pool? There is a default derby pool configured. Maybe you just selected the wrong pool for your resource?
Related
We are migrating from JBoss EAP 6.4 to JBoss EAP 7.0. Our earlier JMS configuration is not working with JBoss 7.0 where it is throwing NameNotFoundException for my JMS beans
my jms.xml
<beans profile="jboss">
<bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jboss/jms/exampleApp/ConnectionFactory" />
<property name="resourceRef" value="true" />
</bean>
<bean id="SampleQueueIn" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="java:jboss/jms/exampleApp/SampleQueueIn" />
<property name="resourceRef" value="true" />
</bean>
</beans>
My messageListener.xml
<beans profile="jboss">
<!-- Abstract MessageListener -->
<bean id="messageListener" abstract="true" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="transactionManager" ref="transactionManager" />
<property name="sessionTransacted" value="true" />
<!-- Receive timeout determines the duration of JMS session. Increase from default 1sec to 5sec, to lower the traffic to Queue managers -->
<property name="receiveTimeout" value="5000" />
</bean>
</beans>
my proxy-domain componentContext.xml which has listener beans
<beans profile="jboss">
<bean id="jmsContainer" parent="messageListener">
<property name="destination" ref="SampleQueueIn" />
<property name="messageListener" ref="sampleInformationListenerImpl" />
</bean>
</beans>
commands.cli part for adding admin objects for wmq
/subsystem=resource-adapters/resource-adapter=${wmq_deploy_name}:activate
/subsystem=resource-adapters/resource-adapter=${wmq_deploy_name}/admin-objects=SampleQueueIn:add(class-name=com.ibm.mq.connector.outbound.MQQueueProxy,jndi-name=java:jboss/jms/exampleApp/SampleQueueIn)
/subsystem=resource-adapters/resource-adapter=${wmq_deploy_name}/admin-objects=SampleQueueIn/config-properties=baseQueueName/:add(value=${filter_jms_queue_config_SampleQueueIn_basequeuename})
/subsystem=resource-adapters/resource-adapter=${wmq_deploy_name}/admin-objects=SampleQueueIn/config-properties=baseQueueManagerName/:add(value=${filter_jms_queue_config_basequeuemanager})
I can see my admin objects in the JBoss for wmq under sub resources with correct jndi-name also.
I am using IBM wmq v9.1.0.2.
[EDIT]
Once I deploy my WAR file, I am getting the below exception
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SampleQueueIn' defined in class path resource [jms.xml]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: jms/exampleApp/SampleQueueIn-- service jboss.naming.context.java.jboss.jms.exampleApp.SampleQueueIn
This configuration was working(still working) in jboss eap 6.4.
I have scourged jboss developer sites & stackoverflow sites but no avail till now!
I can see that my
I resolved the error with reload of JBoss server after running the CLI commands. I automated the execution of both jboss cli commands & reload commands to make sure that the JNDI objects lookup is successful.
Recently I had been discovered Spring Data Jpa. The one thing I was not able to make it working was proper exception translation to Spring's Exception hierarchy.
According to this Spring Data JPA forces CGLib proxying to non repository classes the <jpa:repositories /> activates persistence exception translation for Spring beans annotated with #Repository. The reference documentation in this post points to spring-data-jpa 1.1.1.
But when you look at the docs for version 1.3.0 this paragraph has been removed. Also I was plying with #Repository annotation putting it wherever possible but with no success.
My question is: Is it possible to achieve proper exception translation with the recent spring-data-jpa lib version 1.3.0?
Ok. I will put some configuration here:
...
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="driverClass" value="oracle.jdbc.OracleDriver"/>
<property name="jdbcUrl" value="jdbc:oracle:thin:#localhost:1521:pbase"/>
<property name="user" value="sa"/>
<property name="password" value="pass"/>
</bean>
<context:annotation-config/>
<bean id="myEmf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="myDataSource"/>
<property name="persistenceUnitName" value="prjPersistenceUnit"></property>
<property name="persistenceXmlLocation" value="classpath:META-INF/mpersistence.xml"></property>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"
p:showSql="true"/>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="myEmf"/>
</bean>
<tx:annotation-driven/>
<jpa:repositories base-package="com.mycompany.repository" />
Content of mpersistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
<persistence-unit name="prjPersistenceUnit" transaction-type="RESOURCE_LOCAL">
<description>Persistence unit which uses EclipseLink JPA 2.0 implementation.</description>
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>com.mycompany.Setting</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-server" value="JBoss"/>
<property name="eclipselink.target-database" value="Oracle10g"/>
<property name="eclipselink.weaving" value="static"/>
</properties>
</persistence-unit>
</persistence>
My rpository
#Repository
public interface TestRepository extends JpaRepository<Setting, Long> {
Setting findByNamee(String name);
}
Here findByNamee should rise some Spring database exception as a real property in the databese is name not namee. But I always get
Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.DatabaseException
Though, when configuring a regular Dao object with #Repository annotation everything works as expected.
I am trying to deploy it on Tomcat 6.0 with eclipseLink 2.3.2.
You could check some working example code I published since then at https://github.com/zagyi/examples/tree/master/spring-data-jpa
You have to inject the EclipseLink Jpa Dialect to the EntityManagerFactory, because the exception translator is in the dialect.
I'm working with Glassfish application server and trying to connect my spring-hibernate web application to my db, I have the following configurations:
In Glassfish I have added a jdbc connection and send a successful ping to the DB.
I also added a JDBC resource (in glassfish) with jndi name: jdbc/myResource. This resource is under the connection pool in which I have created and tested (in step 1).
My EntityManaget is annotated with #PersistenceContext:
#PersistenceContext(unitName = "myPU")
protected EntityManager entityManager;
Under \src\main\resources\META-INF\ I created my persistence.xml file:
<persistence-unit name="myPU">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/myResource</jta-data-source>
<properties>
<!-- Glassfish transaction manager lookup -->
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.SunONETransactionManagerLookup" />
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"></property>
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.default_schema" value="PUBLIC" />
<property name="hibernate.format_sql" value="true" />
<!-- Validates the existing schema with the current entities configuration -->
<property name="hibernate.hbm2ddl.auto" value="validate" />
</properties>
</persistence-unit>
This is how my applicationContext looks like:
bean id="txManager"
class="org.springframework.orm.hibernate4.HibernateTransactionManager">
</bean>
<tx:annotation-driven transaction-manager="txManager" />
<jee:jndi-lookup id="emf" jndi-name="persistence/myPU" />
<!-- In order to enable EntityManager injection -->
<bean
class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor">
<property name="persistenceUnits">
<map>
<entry key="myPU" value="persistence/myPU" />
</map>
</property>
</bean>
In my web.xml I have:
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/myPU</persistence-unit-ref-name>
<persistence-unit-name>myPU</persistence-unit-name>
When I publish I keep getting:
No bean named 'myPU' is defined. Please see server.log for more details.
Why is it looking for the persistence unit as a bean?
Any ideas what am I doing wrong?
Thanks in advance
Resolved - I added transaction-type="JTA" to my persistence.xml.
For some reason I was under the impression that if I'm specifying a jta-data-source in my persistence.xml the default transaction-type should be "JTA", maybe it isn't. I don't really understand the relation of the exception I had to this solution but at the moment my persistence.xml has the following line:
<persistence-unit name="myPU" transaction-type="JTA">
And it works like a charm.
I'm trying to configure Spring, JPA and DB2 in order to have the entity manager instance to be used in my spring controllers but according how I have configured Spring this not happens.
These are the two attempts of configuration of spring:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource" />
<bean name="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="em" />
</bean>
<bean id="em"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="fileUtility" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.OpenJpaVendorAdapter">
<property name="database" value="DB2" />
<property name="showSql" value="true" />
</bean>
</property>
</bean>
the second is this:
<!-- Entity manager factory bean. -->
<bean id="entityManagerFactory"
class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
<property name="persistenceUnitName" value="Sample" />
</bean>
<!-- Entity manager bean. -->
<bean id="em" factory-bean="entityManagerFactory"
factory-method="createEntityManager" />
and the entity manager is injected in this way:
<bean id="messageService" class="utilities.services.impl.MessageServiceImpl">
<property name="entityManager" ref="em" />
</bean>
but I have always this exception:
Caused by: java.lang.IllegalArgumentException: methods with same signature createEntityManager() but incompatible return types: [interface com.ibm.websphere.persistence.WsJpaEntityManager, interface org.apache.openjpa.persistence.OpenJPAEntityManagerSPI]
I don't know how can be fixed. Has anyone encountered this problem?
Thanks in advance.
[EDIT]
This is my persistence.xml:
<?xml version="1.0"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="1.0">
<persistence-unit name="fileUtility"
transaction-type="RESOURCE_LOCAL">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<mapping-file>META-INF/mapping.xml</mapping-file>
<properties>
<property name="openjpa.ConnectionURL" value="jdbc:db2://localhost:50000/db2admin" />
<property name="openjpa.ConnectionDriverName" value="COM.ibm.db2.jdbc.app.DB2Driver" />
<property name="openjpa.ConnectionUserName" value="db2admin" />
<property name="openjpa.ConnectionPassword" value="XXXX" />
<property name="openjpa.FlushBeforeQueries" value="true"/>
<property name="openjpa.RuntimeUnenhancedClasses" value="supported" />
</properties>
</persistence-unit>
<persistence-unit name="fileUtility2" transaction-type="JTA">
<provider>org.apache.openjpa.persistence.PersistenceProviderImpl</provider>
<jta-data-source>file_ds</jta-data-source>
<mapping-file>META-INF/mapping.xml</mapping-file>
<properties>
<property name="openjpa.Log" value="SQL=TRACE"/>
<property name="openjpa.ConnectionFactoryProperties" value="PrettyPrint=true, PrettyPrintLineLength=72"/>
</properties>
</persistence-unit>
</persistence>
WebSphere has a JPA implementation bundled. So no need to add openjpa to your lib. In fact, WebSphere is using OpenJPA, so you are not losing anything. Look here for more details
When using a jda-data-source, you need to have transaction-type="JTA". Also, you should not specify connection properties - they are specified in the datasource.
And get rid of the <provider> - the document I linked says:
If no JPA provider is configured in the element of the persistence.xml file within an EJB module, the default JPA provider that is currently configured for this server is used
I believe you're doing the wrong configuration, because you're configuring it "à la Tomcat". If you're using a Java EE application server, such as WAS, you should:
In Spring application context xml file
configure the DAO bean by a <bean> definition
configure the JNDI definition for the datasource created in the application server via a
<jee:jndi-lookup>
definition; the
name
attribute should be
persistence/XXX, where XXX shall match the
<persistence-unit name="XXX" transaction-type="JTA">
in persistence.xml file
The id attribute in the
<jee:jndi-lookup id=YYY> should point to the
name=YYY parameter of the Entity Manager definition in the DAO, this is to say,
#PersistenceContext(name=YYY) EntityManager em;
Specify
<tx:annotation-driven /> and
<tx:jta-transaction-manager />
In file
web.xml of your web app you should include a definition using the xml tag
<persistence-unit-ref> whose
<persistence-unit-ref-name> parameter shall be the
persistence/XXX JNDI name as specified in persistence.xml (shown above).
Finally, you should create a JNDI definition in the application server (AS dependant) that defines the JNDI name for the JDBC connection. This name should match the
<jta-data-source> xml tag in the persistence.xml file, and it is the only link between the JPA definition and the JDBC defined in the application server.
To round up:
Application Context Spring file
<bean class="DAO implementation class" />
<jee:jndi-lookup id="YYY" jndi-name="persistence/XXX" />
<tx:annotation-driven />
<tx:jta-transaction-manager />
persistence.xml file
<persistence-unit name="XXX" transaction-type="JTA">
<jta-data-source>jdbc/DSN</jta-data-source>
</persistence-unit>
web.xml file
...
<persistence-unit-ref>
<persistence-unit-ref-name>persistence/XXX</persistence-unit-ref-name>
</persistence-unit-ref>
...
DAO (only #PersistenceContext shown)
...
#PersistenceContext(name = "YYY")
EntityManager em;
...
Application Server: jdbc/DSN links to the connection definition, where the driver for the DBM is. Depends on both the AS and the DBM used.
Thus, you may see the connection between the DAO -> Spring Application Context file -> persistence.xml and web.xml files -> Application Server JNDI names. IF you're using a full Java EE application server (such as WAS, Weblogic or GlassFish) you don't have to use Spring interface modules; only defnitions in the app server (see Spring documentation, section 12.6.3).
I'm trying to deploy an app using Eclipselink as my JPA Layer on IBM Websphere 7.0.0.9. While trying to do any CRUD operations, i get the following exception:
Caused by: java.lang.IllegalArgumentException: Object: Entity is not a known entity type.
at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.registerNewObjectForPersist(UnitOfWorkImpl.java:4199)
at org.eclipse.persistence.internal.jpa.EntityManagerImpl.persist(EntityManagerImpl.java:380)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:48)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:600)
at org.springframework.orm.jpa.ExtendedEntityManagerCreator$ExtendedEntityManagerInvocationHandler.invoke(ExtendedEntityManagerCreator.java:358)
at $Proxy93.persist(Unknown Source)
... 89 more
Another stack trace i see is :
Caused by: java.lang.ClassCastException: Entity incompatible with Entity
at o.u.d.dao.jpa.converter.impl.EntityBeanConvertorImpl.convertToModel(EntityBeanConvertorImpl.java:143)
... 223 more
My persistence.xml looks like:
<persistence-unit name="ds" transaction-type="JTA"> <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider> <jta-data-source>dsjta</jta-data-source> <class>Entity</class> <exclude-unlisted-classes>true</exclude-unlisted-classes> <properties> <property name="eclipselink.target-server" value="WebSphere_7" /> <property name="eclipselink.logging.level" value="OFF" /> <property name="eclipselink.ddl-generation" value="none" /> <property name="eclipselink.ddl-generation.output-mode" value="database" /> </properties> </persistence-unit>
My application context file looks like:
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> <property name="persistenceUnitName" value="ds"/> <property name="dataSource" ref="dataSource"/> <property name="jpaVendorAdapter"> <bean class="org.springframework.orm.jpa.vendor.EclipseLinkJpaVendorAdapter"> <property name="showSql" value="false"/> <property name="generateDdl" value="false"/> </bean> </property> <property name="loadTimeWeaver"> <bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/> </property> </bean>
Our VM is started with the -javaagent parameter specified and pointing to spring-agent.jar What could be the reason for these exceptions?
The issue here is there is no Spring LoadTimeWeaver for Websphere. When your application attempts to combine a Web-Tier application with the Spring managed EM while using the InstrumentationLoadTimeWeaver class cast exceptions result. EclipseLink supports weaving in all JPA 2 compliant containers but in this case Spring is acting as an intermediary and interfering with the weaving.
Until Spring has a LoadTimeWeaver for Websphere You will need to remove the InstrumentationLoadTimeWeaver and set the EclipseLink persistence.xml property "eclipselink.weaving" to false or use the Static weaver.