I want to know that how can I do two phase commit transaction by using Java EE5…I am using EJB with JPA which has hibernate configured with MySql. I just want to use JAVA EE specification for transaction not using hibernate or JDBC specific object….
Share
All you need to do, to ensure that JTA transactions are used to perform all transactional work in JPA, is to specify that the Persistence Unit type is JTA, and designate a JTA datasource for use by the JPA provider. Your persistence.xml file would have contents similar to the following:
Additionally, you must ensure that the datasource defined in the
jta-data-sourceattribute, does not employ optimizations like allowing local transactions. In simpler words, all transactions involving the said datasource must be XA transactions, or the datasource must be an XA datasource without any support for local transactions.Note that merely specifiying a JTA data source is not sufficient. You must define the persistence unit as one requiring the use of JTA entity managers, as an undefined value for the
transaction-typeattribute, depends on the environment in which the JPA provider operates. If the provider operates in a Java EE environment,JTAentity managers will be created, where asRESOURCE_LOCALentity managers will be created in a Java SE environment.Also, note that, if you specify the
transaction-typeasRESOURCE_LOCAL, then in a Java EE environment, the JPA provider will ignore thejta-data-sourcevalue, and will instead rely on thenon-jta-data-sourcevalue for creating connections.