Glassfish not picking up POSTGRES properties - java

I have EJB3 Entity bean which is to be saved in Postgres DB. I am using Glassfish 3 App server for deployment of the EJB.
When I make the call to EJB using my web layer, the DB call is made but Glassfish throws out the following exception
javax.enterprise.system.std.com.sun.enterprise.server.logging|_ThreadID=135;_ThreadName=Thread-1;|javax.persistence.PersistenceException: org.hibernate.exception.JDBCConnectionException: could not inspect JDBC autocommit mode
at org.hibernate.ejb.AbstractEntityManagerImpl.convert(AbstractEntityManagerImpl.java:1215)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:635)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:589)
at com.cricinfo.session.service.PlayerService.getPlayer(PlayerService.java:41)
whose root cause is
Caused by: org.hibernate.exception.JDBCConnectionException: could not inspect JDBC autocommit mode
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:99)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:52)
at org.hibernate.jdbc.JDBCContext.afterNontransactionalQuery(JDBCContext.java:296)
at org.hibernate.impl.SessionImpl.afterOperation(SessionImpl.java:595)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:1010)
at org.hibernate.impl.SessionImpl.get(SessionImpl.java:998)
at org.hibernate.ejb.AbstractEntityManagerImpl.find(AbstractEntityManagerImpl.java:614)
... 96 more
Caused by: java.sql.SQLNonTransientConnectionException: No current connection.
at org.apache.derby.client.am.SQLExceptionFactory40.getSQLException(Unknown Source)
at org.apache.derby.client.am.SqlException.getSQLException(Unknown Source)
at org.apache.derby.client.am.Connection.getAutoCommit(Unknown Source)
at com.sun.gjc.spi.base.ConnectionHolder.getAutoCommit(ConnectionHolder.java:307)
at org.hibernate.jdbc.ConnectionManager.isAutoCommit(ConnectionManager.java:212)
at org.hibernate.jdbc.JDBCContext.afterNontransactionalQuery(JDBCContext.java:287)
... 100 more
Caused by: org.apache.derby.client.am.SqlException: No current connection.
The connection to the Postgres DB is up and I can view the data using the pgAdmin tool. I can see the query being executed in the logs. From the logs we can see that Glassfish is using a derby client to fetch the connection.
Is there any configuration which I need to make in Glassfish ?
The persistence.xml is :
<persistence-unit name="PlayerApp" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.cricinfo.domain.Player</class>
<properties>
<property name="hibernate.connection.driver_class" value="org.postgresql.Driver"
/> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"
/> <property name="hibernate.hbm2ddl.auto" value="update" /> <property name="hibernate.connection.url"
value="jdbc:postgresql://localhost:5432/PlayerAppDB" /> <property name="hibernate.show_sql"
value="true" /> <property name="hibernate.format_sql" value="true" /> <property
name="hibernate.connection.username" value="postgres" /> <property name="hibernate.connection.password"
value="postgres" />
</properties>
</persistence-unit>
I am able to persist and get data by using a standalone class which directly uses the EntityManagerFactory.
EDIT : SOLUTION
I read about it a bit more and realised that I was doing a mistake in the persitence.xml . Now the persistence.xml looks like this :
<persistence-unit name="PlayerApp" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/DefaultDS</jta-data-source>
<class>com.cricinfo.domain.Player</class>
</persistence-unit>
The transaction type was made to JTA and a Datasource was used instead of the connection properties.
I also logged into the Glassfish Admin console and configured JDBC Connection Pool and JDBC Resources from the tree CommonTasks-> Resources->JDBC
I am able to get the details now.

I read about it a bit more and realised that I was doing a mistake in the persitence.xml . Now the persistence.xml looks like this :
<persistence-unit name="PlayerApp" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/DefaultDS</jta-data-source>
<class>com.cricinfo.domain.Player</class>
</persistence-unit>
The transaction type was made to JTA and a Datasource was used instead of the connection properties.
I also logged into the Glassfish Admin console and configured JDBC Connection Pool and JDBC Resources from the tree CommonTasks-> Resources->JDBC
I am able to get the details now.

Related

H2 database console: how to connect to an embedded H2 JPA database?

I have a couple of unit tests for an application's JPA layer. This JPA layer consists in JPA entities and a service providing the basic API required in order to persist the entities. The unit tets directly use the javax.persistence classes in order to handle the PersistenceManager. Then it tests the persistence API and I can see in the log the SQL statements to create tables and sequences, etc.
The relevant part of the persistence.xml file looks like:
<persistence-unit name="..." transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
...
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test"/>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>
<property name="hibernate.hbm2ddl.auto" value="create-drop"/>
<property name="hibernate.format_sql" value="false"/>
<property name="hibernate.show_sql" value="true"/>
</properties>
</persistence-unit>
...
I have downloaded H2 1.4.200, the Windows installer, and I installed it on Windows 10. Now using the H2 console I want to connect to the database and inspect the tables, sequences, etc. that were created automatically by Hibernate.
So, going to http://localhost:8082 I get the following:
But when I try to connect to my database, using the defined JDBC connection string, I get the following:
What am I doing wrong here ?
Many thanks in advance.
Nicolas
Finally, I've replaced H2 with Oracle.

How to solve "Using Hibernate built-in connection pool (not for production use!)" using JPA i.e. Hibernate EntityManager

I'm new to Hibernate and JPA in general.
I read a lot about this warning, but I still can't solve it.
The answers I read so far, said that it is necessary to have hibernate.cfg.xml in the project.
But I also read that:
If you are using JPA i.e. Hibernate EntityManager, you'll need the persistence.xml. So you generally don't need both as you use either Hibernate proprietary API or JPA.
(what is the purpose of two config files for Hibernate?)
Using persistence.xml I have this warning every time I use Hibernate.
This is my persistence.xml:
<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="integration"
transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<properties>
<property name="hibernate.dialect"
value="org.hibernate.dialect.MySQL5InnoDBDialect" />
<property name="hibernate.hbm2ddl.auto" value="update" />
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/db-name?autoReconnect=true"/>
<property name="hibernate.connection.username" value="root" />
<property name="hibernate.connection.password" value="root" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.id.new_generator_mappings"
value="true" />
</properties>
</persistence-unit>
</persistence>
I can't figure out what I'm doing wrong.
Thanks in advances
It is just a warning stating that you are using a built_in connection pool which is not a suitable solution in the production environment, you should use the application server connection pool in the production environment. depending on your application server you can setup database connection inside your application server then configure hibernate to use that connection.
But if you want to solve this problem without configuring the application server you can see this.

The last packet sent successfully to the server was > 70,400,003 milliseconds ago. is longer than the server configured

I have below exception
javax.persistence.PersistenceException: Exception [EclipseLink-4002]
(Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd):
org.eclipse.persistence.exceptions.DatabaseException Internal
Exception: com.mysql.jdbc.exceptions.jdbc4.CommunicationsException:
The last packet successfully received from the server was 70,400,002
milliseconds ago. The last packet sent successfully to the server was
70,400,003 milliseconds ago. is longer than the server configured
value of 'wait_timeout'. You should consider either expiring and/or
testing connection validity before use in your application, increasing
the server configured values for client timeouts, or using the
Connector/J connection property 'autoReconnect=true' to avoid this
problem.
I did some research and change persistance.xml to this
Latest
<?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="unicorn" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>com.rh.xxx</class>
<properties>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.connection.url" value="jdbc:mysql://xxx:3306/unicorndb?zeroDateTimeBehavior=convertToNull"/>
<property name="hibernate.connection.password" value="xxx"/>
<property name="hibernate.connection.username" value="student"/>
<property name="hibernate.c3p0.max_size" value="100" /> <!--max number of JDBC connections -->
<property name="hibernate.c3p0.min_size" value="10" /> <!--minimum number of JDBC connections-->
<property name="hibernate.c3p0.idle_test_period" value="500" />
<property name="hibernate.c3p0.acquire_increment" value="1" />
</properties>
</persistence-unit>
</persistence>
Did the latest code looked correct?
Any help would be appreciated
To start, keep your testing simple, just use
<property name="hibernate.c3p0.testConnectionOnCheckout" value="true" />
instead of the idle Connection check. See c3p0 docs on Connection testing
If the problem, um, persists then the issue isn't likely to be Connections held by the pool, but some Connection or Connections that your application is checking out and holding open for an indefinite period. Ideally, Connections should be checked out, used, then checked in immediately (and robustly, using try-with-resources or a careful finally block).

Using derby built-in glassfish4 through EclipseLink

I have been trying to learn how to connect to the embedded database Apache Derby that comes out of the box with glassfish4. What do I have to set in the src/META-INF/persistence.xml in my project? Is it somehow preconfigured? If not, how can I change its settings - through glassfish console?
Thanks in advance for your help.
You need to edit the persistence.xml to add the persistence-provider, the classes you want to manage, and some configuration for your database, in case you do not use JTA, in your IDE, and package it with the app. You can generally enter the following in the persistence.xml:
<persistence-unit name="call_it_as_you_want" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>YourClass</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.ClientDriver" />
<property name="javax.persistence.jdbc.url" value="jdbc:derby://127.0.0.1:1527/yourDatabase;create=true" />
<property name="javax.persistence.jdbc.user" value="your_database_login" />
<property name="javax.persistence.jdbc.password" value="your_database_password" />
<property name="eclipselink.ddl-generation" value="drop-and-create-tables" />
</properties>
</persistence-unit>
Before that, check if derby runs on port 1527 - i think it should. If you are going to use JPA with EJBs you can use JTA configuration - in this case persistence.xml will only need to declare the pool you will create from the admin console of Glassfish.

Let the Application Server Handle the DB Connection Pooling

Im using JNDI for datasource defined in our application server(websphere) and it is configured to manage the db connection pool. I have a service deployed on that server which also defines db connection pooling as per the cofiguration below.
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence" version="1.0">
<persistence-unit name="test">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
<property name="hibernate.c3p0.min_size" value="1"/>
<property name="hibernate.c3p0.max_size" value="10"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.format_sql" value="true"/>
<property name="hibernate.ejb.autodetection" value="hbm"/>
<property name="hibernate.use_sql_comments" value="true"/>
</properties>
</persistence-unit>
</persistence>
Now, my goal is to completely remove the db connection pooling management on the service and let the application service handle it. If I remove the two c3p0 entries, does that mean no db connection pooling is happening inside the service and all are managed by the application server?
Im new to this kind of thing, and inputs or reference is highly appreciated. Thanks
[UPDATE1]
From C3P0ConnectionProvider "Hibernate will use this by default if the hibernate.c3p0.* properties are set."
Based on the above xml, I already removed the default pooling. Now if I did not define any pooling provider on the service then I essentially removed the pooling on the service right? I feel this is a dumb question now but please confirm if this is correct. Thanks :)

Categories