I need to migrate a project that uses hibernate from Jboss to weblogic. Currently I'm using this configuration:
persistence.xml
<persistence-unit name="pagosHibernate" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>...</class>
<properties>
<property name="hibernate.ejb.cfgfile" value="META-INF/hibernate.cfg.xml"/>
</properties>
</persistence-unit>
hibernate.cfg.xml
<hibernate-configuration>
<session-factory>
<property name="connection.datasource">MyDS</property>
<property name="jndi.class">weblogic.jndi.WLInitialContextFactory</property>
<property name="hibernate.transaction.manager_lookup_class">org.hibernate.transaction.WeblogicTransactionManagerLookup</property>
<property name="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</property>
<property name="transaction.factory_class">org.hibernate.transaction.JTATransactionFactory</property>
</session-factory>
</hibernate-configuration>
And the code is:
EntityManager em = this.factory.createEntityManager();
EntityTransaction entityTransaction = em.getTransaction();
entityTransaction.begin();
...
em.persist(device);
entityTransaction.commit();
But I'm getting:
java.sql.SQLException: Cannot call commit when using distributed transactions
Thanks
The DataSource "MyDS" used in Hibernate configuration looks like a XA DataSource.
In your case, Non-XA DataSource should be used. While configuring DataSource in Weblogic, you have to choose appropriate thin driver. Hope this helps.
The problem was caused by the XA datasource as R Sawant said. Nevertheless the solution was creating a non-XA datasource and disabling global transactions (keeping global transactions activated persisted the problem).
I would like to know how to configure this project to work on XA databases but that would be another question.
Related
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 :)
I am a newbie in Hibernate. In order to get the transaction by the EntityManager, I need to use EntityManager Factory. When I put this code block on my file:
EntityManagerFactory entityManagerFactory = Persistence
.createEntityManagerFactory("Comment");
EntityManager entityManager = entityManagerFactory.createEntityManager();
EntityTransaction transaction = entityManager.getTransaction();
transaction.begin();
entityManager.persist(obj);
transaction.commit();
I got this Exception:
javax.persistence.PersistenceException: No Persistence provider for EntityManager named Comment
Then I realized that I need to add a persistence.xml file:
<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_1_0.xsd" version="1.0">
<persistence-unit name="QuestionsComments" transaction-type="RESOURCE_LOCAL">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<class>Comment</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
<property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.connection.username" value="root"/>
<property name="hibernate.connection.password" value="root"/>
<property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/sogdb"/>
<property name="hibernate.max_fetch_depth" value="3"/>
</properties>
</persistence-unit>
</persistence>
Actually, The app I am using is not a Java on Server app (I have a Client app). Which means that there is no META-INF folder that will contain the persistence.xml file.
My Questions are:
1- Where I need to put the persistence.xml file?
2- Is the code below OK? Knowing that my table in the database is QuestionsComments and my class that will be related to it using Hibernate is Comment.
If you are using Eclipse, adding the JPA facet to your project allows Eclipse to take care of setting these things up for you. It will create the META-INF folder, and can generate a stub persistence.xml file in it. You don't have to be running the code on a server to have a META-INF folder in your jar file.
When you are creating the EntityManagerFactory the string argument should be the name of the persistence unit. I'd recommend using annotations in your entity class for specifying table name, id column, etc.
Edit: fixed link.
Answered clearly in this section of the Hibernate JPA Quick Start Guide. It goes in META-INF.
Check out the same guide just linked.
Personally I prefer the Hibernate-specific configuration hibernate.cfg.xml with annotations for mappings and Hibernate's SessionFactory for transaction access; I find it easier to wrap my head around -- although that's probably because I originally learned how to use Hibernate without knowing how to use JPA.
I have a project using JBoss + hibernate, and NOT JPA (don't ask me why, just assume this way). So we're using hibernate.cfg.xml and hibernate Session. Well, I'm trying to make JBoss to handle my session, so I changed my hibernate.cfg.xml to something like this:
<hibernate-configuration>
<session-factory name="unitName">
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">false</property>
<property name="hibernate.connection.datasource">MyDS</property>
</session-factory>
</hibernate-configuration>
And in my code I just added:
#PersistenceContext(unitName="unitName") protected Session session;
But when I run the application, it goes:
Caused by: java.lang.IllegalArgumentException: Can't find a persistence unit named 'unitName' in ...
Tried changing from hibernate.cfg.xml to persistence.xml, but still using Session, and I got an error similar to this: https://issues.jboss.org/browse/JBAS-8815
I know, there is a workaround for this error, but my main question is: Can I use a managed Datasource with plain Hibernate/Session and hibernate.cfg.xml? What I'm doing wrong?
Thanks!
I want to use the #PersistenceUnit annotation in my app to create an application managed EntityManager
#PersistenceUnit(unitName="primary")
private static EntityManagerFactory entityManagerFactory;
EntityManager entityManager = entityManagerFactory.createEntityManager();
This doesn't seem to be working. I run my code through a debugger and discover that entityManagerFactory is null. My guess is that the injection of Persistence context with the #PersistenceUnit annotation is not working.
My app is a CDI app. It was not previously a CDI application - I converted it to CDI by creating a beans.xml file in WEB-INF, I needed to in order to do something like this.
Is there anything I need to configure within CDI to get the annotation to work? Thanks.
I have a JPA application running with only Java SE. I don't have a WEB-INF/beans.xml, but I do have a META-INF/persistence.xml configuration file:
<?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="JPAPU" transaction-type="RESOURCE_LOCAL">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<class>jpa.Container</class>
<class>jpa.Item</class>
<properties>
<property name="javax.persistence.jdbc.url" value="jdbc:derby:D:\NetBeansProjects\JPA\jpaTestDB;create=true"/>
<property name="javax.persistence.jdbc.password" value=""/>
<property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
<property name="javax.persistence.jdbc.user" value=""/>
<property name="eclipselink.ddl-generation" value="create-tables"/>
</properties>
</persistence-unit>
</persistence>
Container and Item are the two persistence classes in my jpa package.
This was generated automatically by Netbeans. There is also some information about using JPA without Java EE in the official (Sun/Oracle) Java EE tutorial in the persistence chapter.
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.