Friday, June 24, 2011

JPA: Don't use JPA's RESOURCE_LOCAL on the server

The JPA 1.0 / 2.0 specifications are clear about the JTA or / RESOURCE_LOCAL usage on application servers:

"The transaction-type attribute is used to specify whether the entity managers provided by the entity manager factory for the persistence unit must be JTA entity managers or resource-local entity managers. The value of this element is JTA or RESOURCE_LOCAL. 

A transaction-type of JTA assumes that a JTA data source will be provided—either as specified by the jta-data-source element or provided by the container. In general, in Java EE environments, a transaction-type of RESOURCE_LOCAL assumes that a non-JTA datasource will be provided.

In a Java EE environment, if this element is not specified, the default is JTA. In a Java SE environment, if this element is not specified, the default is RESOURCE_LOCAL."

See section, Page 312 from JSR317

If you deploy the following persistence.xml:

  <persistence-unit name="integration" transaction-type="RESOURCE_LOCAL">
      <property name="javax.persistence.jdbc.url" value="jdbc:derby:memory:testDB;create=true"/>
      <property name="javax.persistence.jdbc.driver" value="org.apache.derby.jdbc.EmbeddedDriver"/>
      <property name="eclipselink.ddl-generation" value="create-tables"/>

into a Java EE application server, you will also have to manage both: the EntityManager and it's JTA-transaction by yourself. This will end up with lots of plumbing. Instead of RESOURCE_LOCAL, you should use the JTA setting in production. With the JTA setting you don't have to specify the JDBC-connection and use a pre-configured JTA data source instead: 

  <persistence-unit name="prod" transaction-type="JTA">
      <property name="eclipselink.ddl-generation" value="drop-and-create-tables"/>


  1. how to setting property of character set in persistence.xml file

    i'm using eclipse link

    and Please note i want to configure it for desktop application

    1. It does indeed seem like EclipseLink has no implementation specific config param for the encoding:

      However, make sure that your project is compiled in UTF-8 format. check that Eclipse is correctlly configured : Window->Preferences->General->Workspace text file encoding is UTF-8. Also if it's the case, configure your Maven project to compile in UTF-8 as well:

      but be sure that the Source/Binary Format is the same as the JDK you use in your IDE,as if you use JDK 1.6 don't make them 1.5, then it should run correctly.

    2. Thank you so much for your replay

      Before i wrote my comment yesterday i tried all of this check and i tried to read file-encoding and tried to write data in file and everything was utf8 and readable but when i tried to insert data into mysql the result is ???? ??? like that and i checked the table and database encoding everything is utf8 and utf8-general-ci

      always the solution was sending parameters to mysql driver like
      prop.put("useUnicode", "yes");
      prop.put("characterEncoding", "ISO-8859-1");

      in jse applications

      but in the jee application i added

      to glassfish-web.xml file
      and everything work fine with properties in datasource "useUnicode" = "yes", "characterEncoding" = "utf8"

      now i want to connect from jse application to mysql via jpa eclipselink but the result is ???? ??? ??
      and i tried to convert String to iso or utf8 before sending to mysql and i got the same problems

      i found the best solution for that in this url by adding to mysql script startup --character-set-server utf8 --collation-server=utf8_general_ci

      Thank you so much for your time