EAR application works on Websphere8.5 but refuses to work on Websphere liberty 16.0.0.4 - jpa

I get in inheritance EAR application which I need to continue to develop. The problem that I can't cause it work on Websphere Liberty 16.0.0.4 while on Websphere Application Server Full Profile 8.5 it works fine. Unfortunately or (fortunately :) ) my working station is Macbook Pro, and WAS Full Profile can't be installed on OSX (can't find links right now, but have done some search and find enough evidences for it) so I need to use VirtualBox with Linux or try to run this app on Liberty.
The latest solution doesn't work so well for me, I get the following error:
[ERROR ] CWWJP0012E: The persistence unit name is not specified and
a unique persistence unit is not found in the BigEnterpriseAppEAR
application and BigEnterpriseAppEJB.jar module. [ERROR ] CWWJP0029E:
The server cannot find the persistence unit in the
BigEnterpriseAppEJB.jar module and the BigEnterpriseAppEAR
application. [ERROR ] CWNEN0035E: The java:comp/env/BigEnterpriseApp
reference of type javax.persistence.EntityManager for the DataProvider
component in the BigEnterpriseAppEJB.jar module of the
BigEnterpriseAppEAR application cannot be resolved. [ERROR ]
CNTR0020E: EJB threw an unexpected (non-declared) exception during
invocation of method "getDataByOwner" on bean
"BeanId(BigEnterpriseAppEAR#BigEnterpriseAppWEB.war#DataAPI, null)".
Exception data: javax.ejb.EJBTransactionRolledbackException: nested
exception is: javax.ejb.EJBException: The
java:comp/env/BigEnterpriseApp reference of type
javax.persistence.EntityManager for the DataProvider component in the
BigEnterpriseAppEJB.jar module of the BigEnterpriseAppEAR application
cannot be resolved.
The app is pretty simple EAR = JPA + EJB + WAR
I don't know which configuration files would be helpful, so just write in a comments what to post and I will do it.
Thank you in advance.
UPDATE 1:
server.xml file:
<server description="new server">
<!-- Enable features -->
<featureManager>
<feature>localConnector-1.0</feature>
<feature>servlet-3.1</feature>
<feature>ejbLite-3.1</feature>
<feature>jndi-1.0</feature>
<feature>jaxrs-1.1</feature>
<feature>ssl-1.0</feature>
<feature>jpa-2.0</feature>
<feature>cdi-1.0</feature>
</featureManager>
<basicRegistry id="basic" realm="BasicRealm">
<!-- <user name="yourUserName" password="" /> -->
</basicRegistry>
<!-- To access this server from a remote client add a host attribute to the following element, e.g. host="*" -->
<httpEndpoint httpPort="9080" httpsPort="9443" id="defaultHttpEndpoint"/>
<!-- Automatically expand WAR files and EAR files -->
<applicationManager autoExpand="true"/>
<applicationMonitor updateTrigger="mbean"/>
<library id="DB2JCC4Lib">
<fileset dir="/Users/anatoly/developer/sql_drivers" includes="*.jar"/>
</library>
<dataSource id="db2_slc" jndiName="jdbc/BEADB" type="javax.sql.DataSource">
<jdbcDriver libraryRef="DB2JCC4Lib"/>
<properties.db2.jcc databaseName="beadb" password="********" portNumber="50000" serverName="db2server" user="db2username"/>
</dataSource>
<keyStore id="defaultKeyStore" password="******"/>
<enterpriseApplication id="BigEnterpriseAppEAR" location="BigEnterpriseAppEAR.ear" name="BigEnterpriseAppEAR"/>
</server>
persistence.xml file, located in BigEnterpriseAppJPA > src > META-INF > persistence.xml
in packaged EAR persistence.xml located in BigEnterpriseAppEAR -> BigEnterpriseAppJPA.jar -> META-INF -> persistence.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="BigEnterpriseApp">
<jta-data-source>jdbc/BEADB</jta-data-source>
<class>com.bea.entities.System</class>
<class>com.bea.entities.Data</class>
<class>com.bea.entities.User</class>
<class>com.bea.entities.Group</class>
<properties>
<property name="openjpa.jdbc.Schema" value="BEADB" />
<property name="openjpa.ConnectionRetainMode" value="transaction" />
</properties>
</persistence-unit>
</persistence>

[ERROR ] CWWJP0012E: The persistence unit name is not specified and a unique persistence unit is not found in the BigEnterpriseAppEAR application and BigEnterpriseAppEJB.jar module.
This would imply your persistence.xml roots are not at the legal locations defined by the JPA spec, section 8.2:
In Java EE environments, the root of a persistence unit must be one of the following:
an EJB-JAR file
the WEB-INF/classes directory of a WAR file[87]
a jar file in the WEB-INF/lib directory of a WAR file
a jar file in the EAR library directory
an application client jar file
NOTE: Java Persistence 1.0 supported use of a jar file in the root of the EAR as the root of a
persistence unit. This use is no longer supported. Portable applications should use the EAR
library directory for this case instead
Your setup seems to be trying to use #4?
BigEnterpriseAppEAR -> BigEnterpriseAppJPA.jar -> META-INF -> persistence.xml
BigEnterpriseAppJPA.jar should be placed inside your EAR library directory. I believe this will be BigEnterpriseAppEAR/lib by default, but you can configure this with your /META-INF/application.xml in the EAR
Also note, that the persistence-unit name must be unique. Make sure that all persistence-unit names are not using the same name.

Related

How to use H2 database with JPA on WebSphere Liberty

I have a very simple web application running on WebSphere Application Server 18.0.0.2. The app is packaged into WAR and put under dropins (for simplicity).
My server.xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<server description="new server">
<featureManager>
<feature>javaee-8.0</feature>
</featureManager>
<httpEndpoint id="defaultHttpEndpoint" httpPort="9080" httpsPort="9443" />
<!-- Automatically expand WAR files and EAR files -->
<applicationManager autoExpand="true"/>
<!-- THE JAR IS THERE (UNDER Liberty lib directory) -->
<library id="H2JDBCLib">
<fileset dir="${wlp.install.dir}/lib" includes="h2-1.4.197.jar"/>
</library>
<!-- AND THIS IS MY DATA SOURCE DEFINITION -->
<dataSource id="h2test" jndiName="jdbc/h2test">
<jdbcDriver libraryRef="H2JDBCLib"/>
<properties.db2.jcc databaseName="testdb" serverName="localhost" portNumber="8082" user="sa" />
</dataSource>
</server>
I have a very simple entity and a service (stateless EJB):
#Stateless
public class CustomerService {
#PersistenceContext(unitName = "h2test")
private EntityManager entityManager;
public List<Customer> getAllCustomers() {
return entityManager
.createNamedQuery(FIND_ALL, Customer.class)
.getResultList();
}
}
And my persistence.xml under META-INF looks like this:
<?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_1_0.xsd"
version="1.0">
<persistence-unit name="h2test" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>jdbc/h2test</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
</persistence-unit>
</persistence>
I thought these simple configs should be enough to be able to deploy and run this hello-world-type of app. But I am getting an error at runtime:
[ERROR ] CNTR0019E: EJB throws an exception when invoking "getAllCustomers".
Details: javax.ejb.EJBException: The java:comp/env/com.my.app.service.CustomerService/entityManager reference of type javax.persistence.EntityManager for the null component in the my-app.war module of the my-app application cannot be resolved.
at com.ibm.wsspi.injectionengine.InjectionBinding.getInjectionObject(InjectionBinding.java:1489)
at [internal classes]
at com.my.app.service.EJSLocalNSLCustomerService_22d8d9f5.getAllCustomers(EJSLocalNSLCustomerService_22d8d9f5.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
EntityManager injection fails. From IBM's docs it's not clear what else should be done.
I have no other XML files (config files) in my app.
Am I missing something ?
The main issue I see is the <datasource> definition in server.xml, you have used the <properties.db2.jcc> element, which corresponds to the IBM DB2 JCC JDBC driver. Since Liberty does not have a dedicated <properties.h2> configuration, you must use the generic <properties> config element, as well as defining the DataSource class names on your <jdbcDriver> element.
The config should look something like this:
<dataSource id="h2test" jndiName="jdbc/h2test">
<!-- Define the DataSource class names on the <jdbcDriver> element -->
<jdbcDriver
javax.sql.XADataSource="org.h2.jdbcx.JdbcDataSource"
javax.sql.ConnectionPoolDataSource="org.h2.jdbcx.JdbcDataSource"
javax.sql.DataSource="org.h2.jdbcx.JdbcDataSource"
libraryRef="H2JDBCLib"/>
<!-- set the connection URL on the <properties> element.
this corresponds to the setURL() method on H2's JdbcDataSource class.
you may also list any properties here that have a corresponding setXXX method on H2's JdbcDataSource class -->
<properties URL="jdbc:h2:mem:testdb"/>
</dataSource>
Also, it would be better if you put your H2 JDBC driver somewhere under ${server.config.dir} or ${shared.resource.dir}, since ${wlp.install.dir}/lib is where jars of the Liberty runtime are. You don't want to mix your application jars in with those!
<library id="H2JDBCLib">
<fileset dir="${server.config.dir}" includes="h2-1.4.197.jar"/>
</library>
Lastly, ensure that your persistence.xml file is in the correct location in your WAR application. It should be at WEB-INF/classes/META-INF/persistence.xml
As an intermediate debugging step, you can check to make sure the DataSource is resolvable from your application like this:
#Resource(lookup = "jdbc/h2test")
DataSource ds;
// ...
ds.getConnection().close();
Once you get that part working, move onto injecting your EntityManager. Also, be sure to check ${server.config.dir}/logs/messages.log for more detailed error messages if things are going wrong.

Persistence unit missing dependencies migrating from JBoss EAP 6.4 to 7.0

I have an application that must run on both JBoss EAP 6.4 and 7.0. The application uses EJB entity beans which are no longer supported on JBoss EAP 7 so the entity beans are being migrated to use JPA entities as described in the JBoss migration guide. The application deploys and works fine on 6.4 but fails to deploy with "missing dependencies" error on 7.0. I've seen alot of issues where the missing dependency was the datasource but that doesn't seem to be the case here. The datasource isn't mentioned at all in the missing dependencies error. I can see in the logs the persistence.xml file is being parsed and the jndi bindings added for the session beans before the deployment fails.
I have an EAR file that contains multiple war and ejb jar files. The application I'm having an issue with consists of an ejbModule.jar which contains the persistence.xml file and entity classes, and a webModule.war file with taglib classes that reference the entity classes contained in ejbModule.jar.
The structure of the EAR file is outlined below:
MyEAR.ear
- ejbJar.jar
- webApp1.war
- webApp2.war
- ejbModule.jar (contains persistence.xml and entity classes)
-- META-INF/persistence.xml
- webModule.jar (taglib classes have dependency on entity classes from ejbModule.jar)
- META-INF
persistence.xml: (replaced class names, datasource name, persistence-unit name)
<persistence version="2.0" xmlns:per="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="myPU">
<class>org.MyEntity1</class>
<class>org.MyEntity2</class>
<jta-data-source>java:/MyDataSource</jta-data-source>
<properties>
<property name="jboss.entity.manager.jndi.name" value="java:/myPU"/>
<property name="jboss.entity.manager.factory.jndi.name" value="java:/myPU"/>
</properties>
</persistence-unit>
It's difficult to post the error message as it's got hundreds of lines of "missing dependencies" for class names and application names that I can't share and by the time I replace them all I don't feel it would be of much use. But there are entries like:
"jboss.naming.context.java.comp.MyEAR.ejbModule.MyEntity1.InAppClientContainer is missing [jboss.naming.context.java.comp.MyEAR.ejbModule.MyEntity1]",
However there also entries like:
"jboss.deployment.subunit.\"MyEAR.ear\".\"webApp1.war\".component.\"org.taglib.MyTagLibClass\".START is missing [jboss.persistenceunit.\"MyEAT.ear/ejbModule.jar#myPU\"]"
I can see from the DEBUG logging for org.jboss.as.jpa that JBoss looks to be adding dependencies on the persistence unit for all taglib classes in all war files in the ear. Only the webModule.war needs to use the persistence unit.
2017-12-07 15:37:13,808 DEBUG [org.jboss.as.jpa] (MSC service thread 1-2) Adding dependency on PU service service jboss.persistenceunit."MyEAR.ear#myPU" for component org.taglib.MyTagLibClass
When I move the persistence.xml file to the META-INF directory of the EAR the application deploys and works fine on JBoss EAP 7.0. However, this isn't an ideal solution as the EAR file is built dynamically and could contain custom applications that I have no control over.
I've tried various other structures to try and get this working but haven't found anything else that works.
Any ideas how to get this working on JBoss EAP 7.0?
There are unnecessary entries in your persistence.xml
You just need this
<persistence version="2.0" xmlns:per="2.1"
xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="myPU">
<jta-data-source>java:/MyDataSource</jta-data-source>
</persistence-unit>
Because the entities will be found automatically relative to persistence.xml
Please try this and if doesn't help please post as much of the server.log as you can.
Another idea could be to ask the RedHat support as you use EAP I assume that you have subscriptions containing support.

Copy files to new project and now cannot create JDBC driver of class '' for connect URL 'null'

Caused by: java.sql.SQLException: No suitable driver
A Maven project in eclipse, I lost the .project file for this application. I have to copy the files into another project. So it was running before.
I am using a DataSource in Tomcat. The projects META-INF/context.xml file hasn't changed. It's deployed into /META-INF directory. web.xml and pom.xml is the same. I don't know what has changed. mysql-connector-5.1.38.jar is in $Catalina/lib.
I would look at the Build Path and Deployment Assembly but let me know.
build path.
deployment assly.
context.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context>
<Resource
name="jdbc/widget"
auth="Container"
type="javax.sql.DataSource"
maxActive="10"
maxIdle="3"
maxWait="10000"
username="appuser"
password="appuserpass"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/widget"/>
</Context>
web.xml
<resource-ref>
<description>MySQL Datasource</description>
<res-ref-name>jdbc/widget</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
Coming back to this, I saw a similar project and matched the deployment assembly. This seemed to fix the issue. Current configuration :
/src/main/java -> WEB-INF/classes
/src/main/resources -> WEB-INF/classes
/src/main/resources/META-INF -> META-INF
/src/main/webapp -> /
/src/test/java -> WEB-INF/classes
Maven Dependencies -> WEB-INF/lib

JBoss (liferay) no context.xml so where to put JNDI resource

I've got the order to switch from Liferay on tomcat, to Liferay on JBoss.
One issue I'm having is that unlike in tomcat, I can't seem to find a context.xml in liferay-portal-6.0.5\jboss-5.1.0\server\default\conf
Will it work if I just copy the context.xml from my tomcat installation to my jboss installation? (I don't know if JBoss scans that folder).
Or is there an alternative location where I can put my resource?
<Resource name="jdbc/x" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="y"
username="z" password="A" maxActive="20" maxIdle="10"
maxWait="-1"/>
Add a file named "*- ds.xml" in the deploy directory server with the following contents:
<?xml version="1.0" encoding="UTF-8"?>
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/myds</jndi-name>
<connection-url>jdbc:oracle:thin:#127.0.0.1:1521:sid</connection-url>
<user-name></user-name>
<password></password>
<new-connection-sql>SELECT * FROM DUAL</new-connection-sql>
<check-valid-connection-sql>SELECT * FROM DUAL</check-valid-connection-sql>
<driver-class>oracle.jdbc.OracleDriver</driver-class>
</local-tx-datasource>
</datasources>

How set jdni.properties on ireport

7.2 with ejbql connection my problem is that when i test the connection, fails because Could not find datasource, in the log says:
Caused by: org.hibernate.HibernateException: Could not find datasource
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
I guees can be the jndi.properties that in the wrong directory, i tried put into java_home/lib but doesn't work
Finally after a lot of work i made the ejbql connection with ireport. Follow this steps to do it!!
1) i'm using jboss 4.2.3, so if you using glashfish or other server, find the libraries that match with the jboss that i'm using
2) You need to find in your jboss directory server/default/lib the follow libraries:
hibernate3.jar
hibernate-entitymanager.jar
jboss-common.jar
hibernate-annotations.jar
ejb3-persistence.jar
jboss.jar
3) copy the libraries previously named and copy your ireport installation directory in this path \Jaspersoft\iReport-3.7.2\ireport\modules\ext and replace it, note that the library call hibernate-common-annotation and jpa.jar need to be erase from that path. You need to do it because this library creates conflicts with hibernate-annotations and ejb3-persistence.jar.
4) get the jar of your project and copy it in your in this example let's called example-core.jar to the installation directory in the path \Jaspersoft\iReport-3.7.2\ireport\libs
5) modify the jar the persistence.xml of your project (in our case "example.jar") and sets with this next properties,
<?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_1_0.xsd"
version="1.0">
<persistence-unit name="example" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<non-jta-data-source>/jdbc/example</non-jta-data-source>
<properties>
<property name="hibernate.connection.driver_class" value="oracle.jdbc.OracleDriver"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle9Dialect"/>
</properties>
</persistence-unit>
</persistence>
6)Go to ireport and add the libraries to the classpath, in tools, options in the tab of classpath
7) go to the installation directory of ireport in the path Jaspersoft\iReport3.7.2\ireport\modules open the jar called com-jaspersoft-ireport with winrar or other tool , and go to META-INF/MANIFEST.INF in the part of Class-Path modify the name of the library "hibernate-common-annotation.jar" (renember you erase this library) for the "hibernate-annotations.jar"
8)Go to Jboss_home/server/default/deploy and modify the datasource xml of your project and put this configuration, (renember in this example the project is call "example" and the datasource should be called example-ds.xml)
<datasources>
<local-tx-datasource>
<jndi-name>jdbc/example</jndi-name>
<use-java-context>false</use-java-context>
<connection-url>jdbc:oracle:thin:#localhost:1521:XE</connection-url>
<driver-class>oracle.jdbc.driver.OracleDriver</driver-class>
<user-name>example</user-name>
<password>example</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.OracleExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>Oracle9i</type-mapping>
</metadata>
</local-tx-datasource>
</datasources>
Note that the property usa-java-context in false is that allow you to access to the data source outside of the jboss in our case from ireport
9)go to the ireport and add the follow libraries into the ireport classpath
jnp-client.jar
jboss-client.jar
jbossall-client.jar
the database driver in my case ojdbc.jar (for oracle)
jboss-ejb3x.jar
10) sets your jndi.properties and put it into the jar hibernate-entitymanager.jar
11) Now run the jboss, go to ireport and create the ejql connection, in the persistence unit name sets the name that place in the persistence.xml of the jar located in installation directory Jaspersoft\iReport-3.7.2\ireport\libs, in this case the persistence unit name is "example" without the quotes marks
I hope that this help to somebody or someone jeje, i know that's pretty hard, and sorry for my english its not my first language