5

I've setted up Hibernate on Glassfish 4.1 but I'm having problems with persistence. I'm able to read data, but cannot write to BD (changes appear to not be commited).

My current persistent.xml looks like this:

 <?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://java.sun.com/xml/ns/persistence/persistence_2_1.xsd">
  <persistence-unit name="myPU" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <jta-data-source>jdbc/myDataSource</jta-data-source>
    <properties>
      <property name="transaction.manager_lookup_class" value="org.hibernate.transaction.SunONETransactionManagerLookup"/>
      <property name="hibernate.transaction.factory_class" value="org.hibernate.transaction.CMTTransactionFactory"/>
      <property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.SunOneJtaPlatform"/>
      <property name="hibernate.dialect" value="org.hibernate.dialect.SQLServer2008Dialect"/>
    </properties>
  </persistence-unit>
</persistence>

My connection pool config on Glassfish is:

<jdbc-connection-pool datasource-classname="com.microsoft.sqlserver.jdbc.SQLServerDataSource" steady-pool-size="2" name="myPool" res-type="javax.sql.DataSource">
  <property name="TrustServerCertificate" value="false"></property>
  <property name="User" value="sa"></property>
  <property name="LastUpdateCount" value="true"></property>
  <property name="ResponseBuffering" value="adaptive"></property>
  <property name="URL" value="jdbc:sqlserver://server\bd"></property>
  <property name="XopenStates" value="false"></property>
  <property name="PacketSize" value="8000"></property>
  <property name="Password" value="mypass"></property>
  <property name="ApplicationName" value="Microsoft JDBC Driver for SQL Server"></property>
  <property name="DatabaseName" value="MyDB"></property>
  <property name="Encrypt" value="false"></property>
  <property name="LockTimeout" value="-1"></property>
  <property name="SendStringParametersAsUnicode" value="true"></property>
  <property name="MultiSubnetFailover" value="false"></property>
  <property name="ApplicationIntent" value="readwrite"></property>
  <property name="LoginTimeout" value="15"></property>
  <property name="WorkstationID" value="My-MacBook-Pro.local"></property>
  <property name="ServerName" value="xpto"></property>
  <property name="PortNumber" value="1433"></property>
  <property name="SelectMethod" value="direct"></property>
  <property name="SendTimeAsDatetime" value="true"></property>
</jdbc-connection-pool>

Datasource config:

<jdbc-resource pool-name="myPool" jndi-name="jdbc/myDataSource"></jdbc-resource>

My EJB looks like this:

@PersistenceContext
private EntityManager em;

public void updateUser(User u) {
        em.merge(u);
}

Any idea how I can fix that?

Thanks!

cнŝdk
  • 31,391
  • 7
  • 56
  • 78
martins.tuga
  • 1,662
  • 1
  • 16
  • 20
  • Hey there, how you're managing your transactions? This looks like it doesn't have a active transaction, or they aren't being committed. Have you tried with JtaTransactionFactory instead of CMTTransactionFactory? Can you post a sample of your EJB where you persist any data? – André Jun 01 '15 at 13:09
  • Hi André, I tried with JtaTransactionFactory and in that case the app hangs (it seems it's waiting for a transaction). My EJBs are using container manager transactions and everything works nice on Wildfly 8.1. – martins.tuga Jun 01 '15 at 13:47
  • Can you provide your jta-data-source configuration on glassfish? – André Jun 01 '15 at 13:51
  • Sure André, it's on the question. Thanks! – martins.tuga Jun 01 '15 at 14:52
  • Can you add how you use your EJB to inject the transaction? I don't see anything wrong so far :/ – André Jun 01 '15 at 17:03
  • André, it's a straightforward Statefull session and as I already told, it was working on Wildfly 8.1. So the problem should be in the EJB – martins.tuga Jun 01 '15 at 17:46
  • 1. Do you see anything in the glassfish server logs when you invoke a transaction? 2. Can you check if the TCP/IP protocol is enabled in your SQL Server instance? 3. What about TCP Dynamic Ports? – PCM Jun 01 '15 at 23:18
  • Hi PCM, I can successfully connect and READ data from SQLServer. My problem is just writing data! And this SQLServer instance is used by other applications. – martins.tuga Jun 02 '15 at 16:57
  • It seems unlikely that the problem is in the persistence.xml if you are able to read from the database. As Andre suggested you may have neglected to commit your transaction after performing the update. – aengus Jun 05 '15 at 14:05
  • Hi @martins.tuga, I'm facing something similar, where did you get the `xml` for the Glassfish's connection pool config? Thanks – Alvaro Pedraza Jun 14 '16 at 14:19
  • Hi @AlvaroPedraza, this xml was a result of many try and fail attempts with some web search. I never solved this problem, and came back to wildfly. – martins.tuga Jun 15 '16 at 05:15

2 Answers2

2

In my case, I was running the Hibernate 5 with tomcat and stop working when I changed to glassfish 4.1

The reason was the oldest jboss-logging.jar at: "YOUR_GLASSFISH_FOLDER/glassfish/modules"

Why? The hibernate 5 has dependency with the newest version of jboss-logging, and the glassfish uses the oldest version even if you declare inside your POM file the newest version. Actually I'm using:

org.jboss.logging jboss-logging 3.3.0.Final

Then I downloaded and replace the old .jar inside modules path and back to work, I spent 2 days trying to solve that and I hope it helps some future issues =D

I used this link to help me: https://medium.com/@mertcal/using-hibernate-5-on-payara-cc242212a5d6#.npq2hdprz

Vitorlui
  • 760
  • 1
  • 11
  • 26
1

Could you please try the following configuration:

    <?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="YOUR_PERSISTANCE_NAME" transaction-type="RESOURCE_LOCAL">
        <provider>YOUR_PROVIDER</provider>

        <!-- ENTITIES -->
        <class>com.company.project....EntityA</class>
        <class>com.company.project....EntityB</class>
        <class>com.company.project....EntityC</class>

        <properties>
            <property name="javax.persistence.jdbc.url" value="YOUR_URL_TO_DB" />
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.user" value="USER" />
            <property name="javax.persistence.jdbc.password" value="PASS" />
            <property name="eclipselink.logging.level" value="INFO" />
            <property name="eclipselink.target-database" value="PostgreSQL" />
        </properties>
    </persistence-unit>
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
Manu Artero
  • 9,238
  • 6
  • 58
  • 73
  • This answer would be better if you explained a little what you are suggesting and why you think that will help. – Lii Jun 01 '15 at 13:35
  • Hi Manu, I'm using hibernate, MSSQLServer and JTA! Your persistence suggestion is very generic. Thanks – martins.tuga Jun 01 '15 at 13:46