Let the Application Server Handle the DB Connection Pooling - java

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 :)

Related

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.

Infinispan - set per Entity expiration.lifespan

I have a java web application deployed on Jboss 6.1.0, that uses infinispan 5.2.6.Final.
I'm trying to set a per Entity specific expiration.lifespan following this guide
http://infinispan.org/docs/5.2.x/user_guide/user_guide.html#_advanced_configuration_2
for my Entity bean com.myenterprise.myproject.dal.ejb.entity.RefStatus.
The guide states the following:
You can also override eviction/expiration settings on a per entity/collection
type basis in such way that the overriden settings only afftect that particular
entity (i.e. com.acme.Person) or collection type (i.e. com.acme.Person.addresses).
For example:
<property name="hibernate.cache.infinispan.com.acme.Person.expiration.lifespan" value= "65000"/>
So, i've added the following element to my persistence.xml, to reduce the lifespan to 10 milliseconds for test purposes, in order to fine tune it later:
<property name="hibernate.cache.infinispan.com.myenterprise.myproject.dal.ejb.entity.RefStatus.expiration.lifespan" value= "10"/>
The setting produces no effects and the lifespan remains the default.
Do you know how I have to set the persistence.xml to successfully override the default expiration lifespan?
Setting the
<property name="hibernate.cache.infinispan.entity.expiration.lifespan" value= "10"/>
it works, but it affects all entity caches, and it is not what I want.
What follows is my application.xml file:
<?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="myProject_dal_PU" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>java:jboss/datasources/myProject-DataSource</jta-data-source>
<shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
<properties>
<property name="hibernate.archive.autodetection" value="class" />
<property name="hibernate.bytecode.use_reflection_optimizer" value="true" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.format_sql" value="false" />
<property name="hibernate.cache.use_second_level_cache" value="true" />
<property name="hibernate.cache.use_minimal_puts" value="true" />
<property name="hibernate.cache.use_query_cache" value="true" />
<property name="hibernate.cache.infinispan.com.myenterprise.myproject.dal.ejb.entity.RefStatus.expiration.lifespan" value= "10"/>
</properties>
</persistence-unit>
</persistence>
Thank you.
If you are running within the application server, apart from the entity name, you have to provide the deployment name and unit too. So, all such expiration properties need to be prepended with: hibernate.cache.infinispan.<warname>.<unitname>.<FQN of entity>...
In your case, I don't know the name of your deployment, but with the unit and FQN that you mention, something like:
hibernate.cache.infinispan.<warname>.myProject_dal_PU.com.myenterprise.myproject.dal.ejb.entity.RefStatus.expiration.lifespan
Spent hours to find correct configuration. Apparently it is as following:
Hibernate property should be in the following format:
hibernate.cache.infinispan.<prefix>.<full-class-name>.<property-name> where:
<prefix> - by default it is in the name that you see in a sort of JNDI name. In case of Wildfly, it is ear-name.ear/ejb-jar-name.jar#persistence-unit-name
However, it can be controlled by hibernate.cache.region_prefix property. Set region_prefix to "" and ignore the prefix.
<property-name> - String as it appears in org.hibernate.cache.infinispan.InfinispanRegionFactory class and in official Infinispan documentation.
In short, official Infinispan documentation is correct only when you set hibernate.cache.region_prefix to ""

Why do we have to give database credentials in persistence.xml

We are using JPA 2.0 and we created the datasource in websphere and tried to access the database through the J2SE application. We are getting Invalid Username and password error. If we give the user name and password in persistence.xml it works fine.
Please anybody explain me why do we have to give the DB credentials in persistence.xml since we have the data source.
Note: Data Source was created successfully and the test was success.
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="Printer">
<jta-data-source>jdbc/TestDataSource</jta-data-source>
<properties>
<property name="openjpa.Optimistic" value="false" />
<property name="openjpa.LockManager" value="pessimistic" />
<property name="javax.persistence.jdbc.user" value="admin" />
<property name="javax.persistence.jdbc.password" value="admin#2" />
</properties>
</persistence-unit>
</persistence>
It seems that the datasource is not configured correctly in websphere. Test the database connection through the websphere console to verify the configuration.
A DataSource has 2 methods to get a Connection. It seems that your jpa implementation uses DataSource.getConnection(String username, String password) if you provide the credentials via properties.
The connection properties are intented to use in a Java SE environment. In JEE you should prefer the JNDI lookup. See section 8.2.1.9 of the JPA 2.0 specification.
You don't need to specify the credentials in your application. Just access your database via JNDI, specifying the name of the datasource you've created in the WS.
One way is to configure persistence unit using the pre-configured datasource (Please double check if it is configured correctly - the test feature is available in WS)
<persistence-unit name="default" transaction-type="JTA">
<provider>
oracle.toplink.essentials.PersistenceProvider
</provider>
<jta-data-source>
jdbc/MyDataSource
</jta-data-source>
<properties>
<property name="toplink.logging.level" value="INFO"/>
</properties>
</persistence-unit>
If the datasource is not configured on the application container side, you may set it up yourself on the application side. For example, you need a number of the applications each one has its own database connection config.
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL">
<provider>
oracle.toplink.essentials.PersistenceProvider
</provider>
<exclude-unlisted-classes>false</exclude-unlisted-classes>
<properties>
<property name="toplink.logging.level" value="INFO"/>
<property name="toplink.jdbc.driver" value="oracle.jdbc.OracleDriver"/>
<property name="toplink.jdbc.url" value="jdbc:oracle:thin:#myhost:l521:MYSID"/>
<property name="toplink.jdbc.password" value="tiger"/>
<property name="toplink.jdbc.user" value="scott"/>
</properties>
</persistence-unit>
Your persistence.xml has the config for both ways. Remove the unnecessary code and try again.

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.

Glassfish not picking up POSTGRES properties

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.

Categories