Can't connect to JDBC database - java

I am trying to run a project in my WAS local server. The problem is I keep getting this error:
Unsupported use of GenericConnection. A GenericConnection is provided
during application start when creating an EntityManagerFactory for a
persistence unit which has configured one of its datasource to be in
the component naming context; java:comp/env. During application start,
the component naming context will not exist, and the correct
datasource cannot be determined. When the persistence unit is used,
the proper datasource and connection will be obtained and used.
In my persistence.xml file I have this:
<persistence-unit name="myPUnit" transaction-type="RESOURCE_LOCAL" >
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<non-jta-data-source>jdbc/myPUnit</non-jta-data-source>
<properties>
<property name="eclipselink.logging.level" value="INFO" />
<property name="eclipselink.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="eclipselink.cache.shared.default" value="false"/>
</properties>
</persistence-unit>
I don't think there's anything wrong with my persistence.xml file, but I keep getting the above mentioned error.
I'm using java 1.8, RAD 9.5 and WAS 8.5.

Related

Having both JPA and Liquibase: what to do with their configuration files?

My project started with JPA only and it doesn't have Spring. Later, I added Liquibase and I had some issues with the persistence unit name since it is necessary to have one to be able to use EntityManager.
entityManagerFactory = Persistence.createEntityManagerFactory("MyPU");
So, to be able to continue with the tables creation with Liquibase and persisting into the database with JPA, I kept both persistence.xml and liquibase.properties files, despite contaning the same database configuration.
<?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="MyPU">
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost/jpa_specialist?createDatabaseIfNotExist=true&useTimezone=true&serverTimezone=UTC"/>
<property name="javax.persistence.jdbc.user" value="root"/>
<property name="javax.persistence.jdbc.password" value="root"/>
<property name="javax.persistence.jdbc.driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
</properties>
</persistence-unit>
</persistence>
changeLogFile=src/main/resources/META-INF/database/dbchangelog.xml
url=jdbc:mysql://localhost/jpa_specialist?createDatabaseIfNotExist=true&useTimezone=true&serverTimezone=UTC
username=root
password=root
I've taken a look at liquibase-hibernate and I didn't understand it very well but it seems to be used to generate the diff files, which is not my need at the moment.
Are both files necessary? Is there something I can do to have only one of them?
Liquibase doesn't have a direct way to read the url/username/password information from the presistence.xml file. The liquibase-hibernate extension does add support for diff'ing a database with your java file mapping files, but doesn't change how Liquibase gets the url/username/password.
You said you were not using Spring, but if you are still in a web application, you can use the Liquibase servlet listener to run Liquibase which pulls the connection from a pre-configured datasource. JPA can pull from that same pre-configured datasource instead of re-defining the configuration as well.
Otherwise, unless you want to do a bit of custom Java coding to parse the persistence.xml file and pass it into Liquibase, you do need both files.
To avoid the repetition you could do something like defining build properties in your maven/gradle/whatever setup and have <property name="javax.persistence.jdbc.url" value="${database.url}"/>in your persistence.xml source file, and url: ${database.url} in your liquibase.properties file.

Create a Derby DB in Glassfish and connect to Java EE application

I'm trying to write a Java EE application that uses JPA to access a database. Until now I just used the #Entity annotation and left everything else to the default state (for example the persistence.xml file was using _TimerPool as the jta-data-source, and I didn't create any db).
So I wanted to try and use an actual database. I went into the Services screen, JavaBD > Create new database, set it up with a name and password.
The DB's url: jdbc:derby://localhost:1527/Prova
Then I created the persistence.xml file for my application through Glassfish's wizard:
<persistence-unit name="JobsPU" transaction-type="JTA">
<jta-data-source>java:app/Prova</jta-data-source>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.schema-generation.database.action" value="create"/>
<property name="javax.persistence.schema-generation.database.target" value="database-and-scripts"/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Prova"/>
<property name="javax.persistence.jdbc.user" value="paolo"/>
<property name="javax.persistence.jdbc.password" value="paolo"/>
</properties>
</persistence-unit>
And when I try to deploy I get this exception:
Grave: Exception while preparing the app : Invalid resource : { ResourceInfo : (jndiName=java:app/Prova__pm), (applicationName=Jobs) }
com.sun.appserv.connectors.internal.api.ConnectorRuntimeException: Invalid resource : { ResourceInfo : (jndiName=java:app/Prova__pm), (applicationName=Jobs) }
Seems to be related to the JNDI naming. Of what I honestly don't know, I'm still trying to learn. If I go to Glassfish's console, under the JNDI listing I can't see anything that seems to be related to my database (not in JDBC Connection Pools nor in JDBC Resources). What should I do?
Thank you very much in advance for any help.
To configure PersistenceUnit for EE container you have to use jta-data-source and transaction-type="JTA". For jta-data-source you have to specify JNDI name of JDBC ConnectionPool which have to be configured in EE container (glassfish server in your case). There is tutorial How to set up a JDBC Connection Pool on Glassfish. Also in this case PersistenceUnit properties like
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Prova"/>
<property name="javax.persistence.jdbc.user" value="paolo"/>
<property name="javax.persistence.jdbc.password" value="paolo"/>
will be ignored for EE container (see this documentation).
To use this properties you can configure PersistenceUnit for SE environment. Before this i recommend you to read article differences between RESOURCE_LOCAL and JTA persistence contexts.
Configuration for SE application will be look like:
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>
org.eclipse.persistence.jpa.PersistenceProvider
</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:derby://localhost:1527/Prova"/>
<property name="javax.persistence.jdbc.user" value="paolo"/>
<property name="javax.persistence.jdbc.password" value="paolo"/>
</properties>
</persistence-unit>

Two Persistence Units in persistence.xml - One works, other not

I'm having such a strange problem with an JPA application in java. I'm trying to read data from a MySQL database and write it on a ObjectDB embed database but when i try to open the Persistence unit i got this message:
Exception in thread "main" javax.persistence.PersistenceException: No Persistence provider for EntityManager named itunes_puSQL
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:85)
at javax.persistence.Persistence.createEntityManagerFactory(Persistence.java:54)
at br.com.lagranzotto.itunes.parser.main.iTunesParser.read(iTunesParser.java:78)
at br.com.lagranzotto.itunes.parser.main.iTunesParser.main(iTunesParser.java:72)
My persistence.xml as follows:
<persistence-unit name="itunes_pu" transaction-type="RESOURCE_LOCAL">
<provider>com.objectdb.jpa.Provider</provider>
<class>br.com.lagranzotto.itunes.parser.model.Album</class>
<class>br.com.lagranzotto.itunes.parser.model.Artist</class>
<class>br.com.lagranzotto.itunes.parser.model.Cover</class>
<class>br.com.lagranzotto.itunes.parser.model.Track</class>
<properties>
<property name="javax.persistence.jdbc.url" value="objectdb:itunes.odb"/>
</properties>
</persistence-unit>
<persistence-unit name="itunes_puSQL" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>br.com.lagranzotto.itunes.parser.model.Album</class>
<class>br.com.lagranzotto.itunes.parser.model.Artist</class>
<class>br.com.lagranzotto.itunes.parser.model.Cover</class>
<class>br.com.lagranzotto.itunes.parser.model.Track</class>
<properties>
<property name="eclipselink.jdbc.password" value="**************"/>
<property name="eclipselink.jdbc.user" value="itunes"/>
<property name="eclipselink.jdbc.driver" value="com.mysql.jdbc.Driver"/>
<property name="eclipselink.jdbc.url" value="jdbc:mysql://localhost:3306/itunes"/>
<property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>
<property name="eclipselink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
</persistence>
I can't have more than one persistence unit per application?
The exception is not related to the first persistence unit or for having more than one persistence unit. Using multiple persistence units in one application is supported and allowed by JPA.
The error message indicates that JPA cannot find a persistence provider (i.e. a JPA implementation) that can handle the itunes_puSQL persistence unit. More specifically, class org.eclipse.persistence.jpa.PersistenceProvider, which is part of EclipseLink (and specified in your XML as the provider) is not found.
As suggested above, check your classpath. Make sure that both EclipseLink and ObjectDB are in the classpath.

org.hibernate.HibernateException: Could not find datasource

I am using JPA with Hibernate implementation and i have created a datasource in websphere server. I am trying to access this datasource in my JPA application. But, when i try to access , i am getting the below exception . Please help me out if you have come across such situation. Thanks
org.hibernate.HibernateException: Could not find datasource
Caused by:
javax.naming.NameNotFoundException: Name comp/env/jdbc not found in context "java:".
Please find the below details:
Datasource Name: pieportaldev_0
JNDI Name: pie/logDB
And my Persistance.xml is
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<!-- the JNDI data source -->
<jta-data-source>java:comp/env/jdbc/pieportaldev_0</jta-data-source>
<class>com.test.jpa.UserInfo</class>
<properties>
<!-- if this is true, hibernate will print (to stdout) the SQL it executes, so you can check it to ensure it's not doing anything crazy -->
<!-- <property name="hibernate.show_sql" value="true" /> -->
<property name="hibernate.format_sql" value="true" />
<!-- since most database servers have slightly different versions of the SQL, Hibernate needs you to choose a dialect so it knows the subtleties of talking
to that server -->
<property name="hibernate.dialect" value="org.hibernate.dialect.OracleDialect" />
<!-- this tell Hibernate to update the DDL when it starts, very useful for development, dangerous in production -->
<property name="hibernate.hbm2ddl.auto" value="update" />
</properties>

Very low performance with Glassfish 3.1.2 and Java EE + Hibernate

I faced with very very low performance of Java EE (EJB + JSF) application and Hibernate(3.6.8.Final and 4.1.7.Final) on Glassfish 3.1.2. Sending about 300 select queries takes about 20 seconds. This is unacceptable.
I have exactly the same application deployed on JBoss and TomEE. There, the same 300 select queries takes about 1,5 second.
I found in google some answers that maybe hibernate.show_sql is true or hibernate.hbm2ddl make application soo slow. But it is not true. I turned off hibernate.show_sql but is doesn't matter. Moreover, these options are true in the JBoss and TomEE versions and it works over 10 times faster!
I thought that this is the issue between Glasfish and Hibernate. But I have the next application with the same business logic, the same DAO with EntityManager provided by Hibernate but configurated with Spring. And the performance is great. It is weird, isn't it?
persistence.xml from the defect version:
<?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" version="1.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd">
<persistence-unit name="jee_project" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/PostgreSQL</jta-data-source>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
<property name="hibernate.show_sql" value="false"/>
<property name="current_session_context_class" value="thread"/>
</properties>
</persistence-unit>
</persistence>
Glassfish JDBC configuration
<jdbc-connection-pool driver-classname="" datasource-classname="org.postgresql.ds.PGConnectionPoolDataSource" res-type="javax.sql.ConnectionPoolDataSource" description="" name="PostgreSQLPool">
<property name="User" value="postgresql"></property>
<property name="DatabaseName" value="qazxsw"></property>
<property name="LogLevel" value="0"></property>
<property name="Password" value="1234"></property>
<property name="ServerName" value="localhost"></property>
<property name="Ssl" value="false"></property>
<property name="ProtocolVersion" value="0"></property>
<property name="TcpKeepAlive" value="false"></property>
<property name="SocketTimeout" value="0"></property>
<property name="PortNumber" value="5432"></property>
<property name="LoginTimeout" value="0"></property>
<property name="UnknownLength" value="2147483647"></property>
<property name="PrepareThreshold" value="5"></property>
</jdbc-connection-pool>
<jdbc-resource pool-name="PostgreSQLPool" description="" jndi-name="jdbc/PostgreSQL__pm"></jdbc-resource>
<jdbc-resource pool-name="PostgreSQLPool" description="" jndi-name="jdbc/PostgreSQL__nontx"></jdbc-resource>
Is your transaction in read-only mode. When your session contains a lot of objects, hibernate can take huge time doing it's automatic dirty checking. Maybe your transaction is in read-only mode under tomcat/jboss but not under glassfish
I found the cause. The answer was hidden in the listings which I pasted above. Three months ago I deployed the same application on Glassfish. Then I used Glassfish first time. I found at some blog how to set datasource (at localhost:4848). Suppose my datasource was named jdbc/PostgreSQL. Then I get the exception that glassfish cannot find datasource jdbc/PostgreSQL__pm. Somewhere in Internet I found info that suffix __pm is needed. Next exception was about suffix __nontx. After changing the names, the application started. But also with very low performance.
Now I added datasource named jdbc/PostgreSQL and it starts working with good performance!
How it is possible that name of datasource was wrong, and in spite of all it was working (slowly)?

Categories

Resources