How do I use Hibernate along with WildFly? - java

good evening.
I'm trying to create a project that uses:
Java
JSF 2.3
Maven
CDI 2.0
Hibernate
Wildfly server
My intention here is to learn the basics of all these fellas.
At first I followed an awesome [tutorial by #BalusC][1] that tought me how to set up and first run the webapp.
My problem now rests on integrating the database with the application. I followed some guides and searched a bit on internet and found that a way to do it is by editing web.xml and persistence.xml. (I tried to keep it as close to hist tutorial as possible).
Here are my web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0" metadata-complete="true">
<display-name>project</display-name>
... (some params)
<data-source>
<name>java:global/projectDS</name>
<class-name>org.postgresql.ds.PGConnectionPoolDataSource</class-name>
<url>jdbc:postgresql://localhost:5432/project</url>
</data-source>
... (servlet info)
</web-app>
And my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence 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_2.xsd"
version="2.2">
<persistence-unit name="projectPU"
transaction-type="JTA">
<jta-data-source>java:global/projectDS</jta-data-source>
<class>project.entity.Message</class>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.PostgreSQL95Dialect" />
<property name="hibernate.default_schema" value="main" />
<property name="hibernate.hbm2ddl.auto" value="create" />
<property name="javax.persistence.jdbc.driver"
value="org.postgresql.Driver" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
</properties>
</persistence-unit>
</persistence>
When I run the web app I receive the following error:
Caused by: org.jboss.as.controller.OperationFailedException: WFLYJCA0117: org.postgresql.ds.PGConnectionPoolDataSource is not a valid javax.sql.DataSource implementation [ "WFLYJCA0117: org.postgresql.ds.PGConnectionPoolDataSource is not a valid javax.sql.DataSource implementation" ]
I'd like to know if any of you know what I'm doing wrong and how can I work it out.
P.S.: I read on WildFly's page that WildFly has it's own Hibernate "version", and some foruns said that there might be some issues while working with an "external" hibernate source. However, as my Hibernate configuration has nothing linked to any of Wildfly's config (i believe so), I guess that's not the problem.
Thanks in advance.
UPDATE #1
I tried solution #2 from link #TacheDeChoco sent. At first it worker, but I got another error that I'm still trying to solve. I'll try I little bit more and will come here If a more complete feedback.
Ansewring your questions: by the time I first asked, I hadn't done any of the things you asked.
TY very much.

Did you
register a new postgres module (with the appropriate jar) in Wildfly ?
Declare the datasource, along with the used driver, in wildfly config file (standalone-***.xml) ?
Nice explanations can be found here (see option#2):
https://www.stenusys.com/how_to_setup_postgresql_datasource_with_wildfly/

Related

No Persistence provider for EntityManager named- when try to deploy from weblogic console

I'm struggling for a few days about this error.
When I try to deploy from ide(IntelliJ-eclipse), it works correct.
But when I try to deploy from WebLogic console, I got these errors;
Error Unable to access the selected application.
Error Unable to invoke Annotation processoror.
Error Unable to invoke Annotation processoror.
For more information, I checked logs and errors from stack trace.
It looks like the main error is: javax.persistence.PersistenceException: No Persistence provider for EntityManager named.
In debug, throwing from this code; Persistence.createEntityManagerFactory("persistanceUnitName");
Persistence classes are coming from javaee-web-abi-7.0.jar.
Everything looks fine in 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="persistanceUnitName"
transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>dataSource</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.target-database" value="Oracle" />
<property name="eclipselink.logging.parameters" value="true" />
<property name="eclipselink.logging.logger" value="DefaultLogger" />
<property name="eclipselink.logging.level" value="WARNING" />
<property name="eclipselink.refresh" value="true" />
<property name="eclipselink.query-results-cache" value="false" />
<!-- <property name="eclipselink.ddl-generation" value="create-or-extend-tables" />-->
</properties>
</persistence-unit>
</persistence>
persistance.xml located in /WEB-INF/classes/META-INF
We got a case like that: we have a test and prep environment. In test, it works, in prep doesn't work, in local doesn't work (from WebLogic console). Unfortunately, I can't see test server configs. But I expect the same options with prep. Anyway forget other environments, firstly I need to deploy successful from local.
Could you pls help me, I really don't know what I miss. Read every topic, tried everything
It looks like a classpath issue. Check your weblogic classpath.
If you are using eclipselink library must be loaded first when you are deploying.
Check this.
Add eclipselink into your project with scope provided:
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.5.0</version>
<scope>provided<scope>
</dependency>
add same dependency into your weblogic classpath:
$WEBLOGIC_HOME/user_projects/domains/base_domain/lib
Then, add below to your weblogic.xml to use eclipselink library.
<wls:container-descriptor>
<wls:prefer-application-packages>
<wls:package-name>org.eclipse.persistence.*</wls:package-name>
</wls:prefer-application-packages>
</wls:container-descriptor>

correct way to include a database connection to POSTGRES in payara micro war file

I managed to deploy and use a JPA connection using the below files.
This is currently working, but it's not ideal.
The connection should be using the postgres pool driver, and a JNDI data source rather than a data source in web.xml.
What is the best practice way of deploying a database connection in a JEE container.
Here is my current setup, using a global connection that is not using the pooled driver.
persistence.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
version="2.1"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
<persistence-unit name="pcc" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:global/pccData</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="eclipselink.deploy-on-startup" value="true"/>
<property name="eclipselink.logging.level" value="INFO"/>
<property name="eclipselink.logging.level.sql" value="CONFIG"/>
<property name="eclipselink.jdbc.fetch-size" value="1000"/>
<property name="eclipselink.jdbc.cache-statements" value="true"/>
<property name="eclipselink.persistence-context.flush-mode" value="commit"/>
<property name="eclipselink.ddl-generation.output-mode" value="database"/>
</properties>
</persistence-unit>
</persistence>
web.xml
I am sure PGSimpleDataSource is not the recommended approach
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<display-name>PCC Web Application</display-name>
<data-source>
<name>java:global/pccData</name>
<class-name>org.postgresql.ds.PGSimpleDataSource</class-name>
<server-name>localhost</server-name>
<port-number>5432</port-number>
<database-name>pcc</database-name>
<user>postgres</user>
<password>postgres</password>
<property>
<name>fish.payara.slow-query-threshold-in-seconds</name>
<value>5</value>
</property>
</data-source>
</web-app>
To answer your question:
What is the best practice way of deploying a database connection in a JEE container?
This is pretty much it.
It doesn't need to be any more complex than the set up you currently have. I assume you have already seen the official Payara-Examples repository but, if not, there is a JPA example there which is configured exactly as you have in your question:
https://github.com/payara/Payara-Examples/blob/master/Payara-Micro/jpa-datasource-example/ReadMe.md
As a general point about configuring Payara Micro, you can think of it as a Payara Server in a different package, so if you're still unsure about something and want to do things in the same way as in the more traditional server then you can do.
There are --postbootcommandfile and --postdeploycommandfile options which will allow you to run asadmin commands against Payara Micro and configure it just like Payara Server.
For your connection pool example, if you really wanted to define it in the server (really, defining it in the web.xml as you already have done is more portable and would be my preferred option), then you could start a normal Payara Server, click the button to enable asadmin command recording in the admin console, and then make the changes using the GUI. The necessary commands will then by dumped to a file for you to apply to Payara Micro later.
Payara Micro also dumps out a temporary directory (controllable with --rootDir, by default has the same value as java.io.tmpdir) so you can always see how a configuration change has affected it by inspecting the domain.xml in that directory structure. This gives you some form of manual verification.

JBoss EAP 6.3 CMT Multiple persistence units

This is essentially a duplicate of How to locate the source of JBAS011470 error in JBoss?
But essentially, As soon as I add a second persistence unit, it gives me this error. It's ridiculous. I'm not going to disable the JPA subsystem like some people suggest - that sounds wrong.
My persistence.xml setup is as follows, where java:/NAME is set up as a datasource in standalone.xml:
<?xml version="1.0"?>
<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="name" transaction-type="JTA">
<jta-data-source>java:/NAME</jta-data-source>
<class>za.co.classes.A</class>
<class>za.co.classes.B</class>
<class>za.co.classes.C</class>
<properties>
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform"/>
<property name="hibernate.transaction.manager_lookup_class"
value="org.hibernate.transaction.JBossTransactionManagerLookup"/>
<property name="jboss.entity.manager.factory.jndi.name"
value="java:jboss/persistence/NAME" />
<property name="jboss.entity.manager.jndi.name"
value="java:jboss/persistence/em/NAME" />
<property name="hibernate.dialect" value="za.co.equrahealth.dao.SQLServerDialect" />
</properties>
</persistence-unit>
The error starts as soon as I add a second persistence unit. So spring context is irrelevant.
Well, I luckily have multiple databases within the same schema, so I came up with a workaround. But it's obviously not going to solve the problem when there are multiple schemas. I think the cause of this issue might actually be a bug in JBoss.
#PersistenceContext
private EntityManager entityManager;
private EntityManager getEntityManager(String source)
{
if ("a".equalsIgnoreCase(source))
{
entityManager.createNativeQuery("USE A_DB;").executeUpdate();
}
else
{
entityManager.createNativeQuery("USE B_DB").executeUpdate();
}
return entityManager;
}

Ljava.lang.String class cannot be resolved in persistence.xml

I am using JBoss Developer Studio. I have a project with a persistence.xml file. The file is perfect to me, but I keep getting this error on the tab that lists all the Problems of my project.
Class "[Ljava.lang.String;#22ec7158" cannot be resolved
I include the picture for a better context.
When I click on the error, so that it takes me to the place where the error is happening, it takes me to the end of the file.
Persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="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="Persistence">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/MemberOfferDS</jta-data-source>
<class>com.bbb.memberoffer.model.SycsCoordinator</class>
<class>com.bbb.aicweb.memberoffer.model.SycsCoordinatorPhoneNumber</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorClub</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorSecurityGroup</class>
<class>com.bbb.memberoffer.model.SycsCoordinatorClubPk</class>
<class>com.bbb.memberoffer.model.PhoneNumberType</class>
<class>com.bbb.memberoffer.model.Club</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<!-- Not sure if this is the right one to use or not? -->
<property name='hibernate.show_sql' value='true' />
<property name='hibernate.format_sql' value='true' />
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServerDialect"/>
</properties>
</persistence-unit>
</persistence>
You may have solved this already, however, in my case the problem was caused by referencing a class in a element that no longer existed.
EDIT: The exact reason for this type of error message is that the error generator is trying to run a toString() on a String array when it generates the error message.

My persistence Unit is pinning to the fjords, when used for a remote connection

While i does manage to connect to my database through the "Services" netbeans tab, my application persistent unit seems to not be working... It just seems to go in an endless "Wait some more, maybe we'll connect you later..." cycle.
Question 1: Why isn't it working?
Question 2 (if no reasons for 1): How can i produce a small work-around?
Here is my persistent unit statement:
entityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory(resourceMap.getString("entityManager.persistenceUnit")).createEntityManager();
The resourceMap.getString("entityManager.persistenceUnit")just allow me to access the property field which define my persistence unit.
Here is my persistence.xml:
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="1.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_1_0.xsd">
<persistence-unit name="analyses_2PU" transaction-type="RESOURCE_LOCAL">
<provider>oracle.toplink.essentials.PersistenceProvider</provider>
<class>analysesmanager.Produits</class>
<class>analysesmanager.Lots</class>
<class>analysesmanager.SpecProduits</class>
<class>analysesmanager.Parametres</class>
<class>analysesmanager.Clients</class>
<class>analysesmanager.Resultats</class>
<class>analysesmanager.Analyses</class>
<properties>
<property name="toplink.jdbc.user" value="XXXXX"/>
<property name="toplink.jdbc.password" value="XXX"/>
<property name="toplink.jdbc.url" value="jdbc:mysql://172.17.22.15:3306/soliance_analyses"/>
<property name="toplink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
</properties>
</persistence-unit>
</persistence>
While this doesn't work with my distant mysql server, it works on my local server using a local copy of the database...
The only difference is between those 2 lines...
<property name="toplink.jdbc.url" value="jdbc:mysql://localhost:3306/soliance_analyses"/>
<property name="toplink.jdbc.url" value="jdbc:mysql://172.17.22.15:3306/soliance_analyses"/>
By the way, running my local version, directly on the remote server does work... It seems to be an issue with either the network (which would not be solvable), either the persistence.xml used for remote connection.
Small work-around:
I have put my project local version on the remote server, and i now run it locally on the remote server, through a .bat .
Here is the ROFLMAO_it_works.bat
net use X: \\remote_server_ip\project_jar_path
X:
java -jar X:\project.jar
It works. Not the way i wanted at the very beginning, but it works.

Categories

Resources