org.hibernate.HibernateException: Could not find datasource - java

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>

Related

Issue setting FlushMode.COMMIT in Quarkus Hibernate ORM

Issue I have using Quarkus+Hibernate:
For performance purposes we need to set FlushMode as COMMIT in our hibernate Session.
And we realized that this property configuration is not available in application.properties parameters, see: https://quarkus.io/guides/hibernate-orm#hibernate-configuration-properties
So, we took the path to setting up the configuration with persistence.xml file: https://quarkus.io/guides/hibernate-orm#persistence-xml
...
<persistence-unit name="SomethingPU" transaction-type="JTA">
<description>Something Entities</description>
<properties>
<!-- Connection specific -->
<property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2012Dialect" />
<!-- cache properties -->
<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"/>
<!-- multitenancy -->
<property name="hibernate.multiTenancy" value="DATABASE" />
<!-- scan for annotated classes -->
<property name="hibernate.archive.autodetection" value="class"/>
<!-- performance tunning -->
<property name="org.hibernate.flushMode" value="COMMIT" />
<property name="hibernate.jdbc.batch_size" value="100" />
<property name="hibernate.jdbc.fetch_size" value="400" />
<property name="hibernate.order_updates" value="true" />
<property name="hibernate.order_inserts" value="true" />
<property name="hibernate.max_fetch_depth" value="1" />
</properties>
</persistence-unit>
When we start our service everything's fine, the properties are loaded into the Session, except for "org.hibernate.flushMode" parameter.
Debugging the code we see this behaviour:
when the service starts, Quarkus executes the Recorder: io.quarkus.hibernate.orm.runtime.HibernateOrmRecorder
this class initialize org.hibernate.Session using the class: io.quarkus.hibernate.orm.runtime.TransactionSessions
TransactionsSessions mantains a Map of io.quarkus.hibernate.orm.runtime.session.TransactionScopedSession
so, when TransactionScopedSession acquires the Session, it executes:
TransactionScopedSession.aquireSession
line 88:
Session newSession = jtaSessionOpener.openSession();
which ends calling JTASessionOpener.createOptions method:
return sessionFactory.withOptions()
.autoClose(true) // .owner() is deprecated as well, so it looks like we need to rely on deprecated code...
.connectionHandlingMode(
PhysicalConnectionHandlingMode.DELAYED_ACQUISITION_AND_RELEASE_BEFORE_TRANSACTION_COMPLETION)
.flushMode(FlushMode.ALWAYS);
Here JTASessionOpener is setting flushMode as ALWAYS
calling the method: SessionFactoryImpl.flushMode
When org.hibernate.internal.SessionImpl is created
org.hibernate.internal.SessionImpl.SessionImpl(SessionFactoryImpl, SessionCreationOptions)
line 266:
if ( getHibernateFlushMode() == null ) {
final FlushMode initialMode;
if ( this.properties == null ) {
initialMode = fastSessionServices.initialSessionFlushMode;
}
...
The method getHibernateFlushMode() is returning FlushMode.ALWAYS
And because of this, the parameter "org.hibernate.flushMode" from persistence.xml is never setted.
We fix this issue setting the flushMode directly in javax.persistence.EntityManager instance, before we use it in any query or DB operation.
But, my questions are:
is there a way to bypass JTASessionOpener or use another logic? can it be considered as a bug or issue to solve?
is there a better way (than our solution) to fix this issue?
is there a plan to add the property "org.hibernate.flushMode" in Quarkus Hibernate ORM extension?, so we can set this in application.properties
--
Hope the description is clear.

Can't connect to JDBC database

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.

multiple persistence unit eclipselink

I m using an osgi application with postgresql connection and a persistence.xml that containt some properties like
<persistence-unit name="postgres-name"
transaction-type="JTA">
<properties>
<!-- <property name="show_sql" value="true" /> -->
<!-- <property name="hibernate.format_sql" value="true" /> -->
<property name="eclipselink.logging.level" value="FINEST"/>
<property name="eclipselink.logging.level.sql" value="FINEST"/>
...many other properties...
</properties>
</persistence-unit>
And in another bundle i want to initialize a H2database with its own unitname, so i have declared a new persistence.xml with :
<persistence-unit name="h2-unitname"
transaction-type="RESOURCE_LOCAL">
<properties>
...many properties...
<property name="eclipselink.allow-zero-id" value="true"/>
...many other properties...
Problem is when i get EntityManager for postgres database, all work fine, next i get h2 EntityManager and work fine, but next time postgres EntityManager is used, i have an error :
Detail: key (id)=(0) already exist
Call: INSERT INTO TABLE(ID, TEST) VALUES (?, ?)
bind => [0, null]
I think my property eclipselink.allow-zero-id define in h2 unitname ovveride the postgres EntityManager...
In fact if i use always postgres database with 0 id, all wark fine, because sequence is call, but if i use one time the h2 EntityManager, entity with 0 id don't work anymore because of allow-zero-id.
How can i define separate property for each persistent unitname?
Ask me if you want to see more configuration
thanks by advance

hibernate reverse engineering persistence unit not found

I havea java maven3 project in Eclipse IDE with jboss tools installed. I'm using hibenrate4
I'm triing to setup the hibenrate configuratoin in hibernate view to test hql queries, the problem is that is says that it couldnt find the persistence unit. I have the persistence.xml placed in src/main/resources/META-INF/persistence.xml
is there some maven config that I nead to set?
Persistence.xml
<persistence-unit name="ypay">
<provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/ypay</jta-data-source>
<properties>
<!-- Properties for Hibernate -->
<!-- <property name="hibernate.hbm2ddl.auto" value="validate" /> -->
<property name="hibernate.show_sql" value="false" />
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect" />
</properties>
hibernate.properties
hibernate.connection.password=
hibernate.connection.username=root
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.dialect=org.hibernate.dialect.MySQLInnoDBDialect
hibernate.connection.url=jdbc:mysql://****************:3306/ypay?useUnicode=true&characterEncoding=UTF-8
hibernate.connection.provider_class=org.hibernate.connection.DriverManagerConnectionProvider
hibernate.datasource=
hibernate.transaction.manager_lookup_class=
You need a META-INF/persistence.xml with
<persistence-unit name="ypay">
(usually put it in WEB-INF/classes/META-INF. When using maven you can try placing it in src/main/resources/META-INF)
Please refer to this doc for details.

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