Persistence Context Transaction
marcusdidiusfalco Dec 2, 2009 3:33 AMI am experimentig with JPA and I have difficulties to reproduce an example taken from JavaMagazin 9/09 about the ECB Pattern.
JBoss 5.1.0 GA, MySQL 5.0
The "boundary" (facade)
@Stateless public class OrderServiceBean implements OrderService { @EJB DeliveryService deliveryService; @EJB Warehouse warehouse; @TransactionAttribute(TransactionAttributeType.REQUIRED) public Order order(Order newOrder) throws BillingException { Order order = warehouse.checkout(newOrder); deliveryService.deliver(order); return order; } }
The Warehouse Bean persists the order. This works like expected:
@Stateless public class WarehouseBean implements Warehouse { @PersistenceContext(unitName="ECB") EntityManager entityManager; @TransactionAttribute(TransactionAttributeType.MANDATORY) public Order checkout(Order order) { this.entityManager.merge(order); return order; } }
However the DeliveryService modifies the order:
@Stateless public class DeliveryServiceBean implements DeliveryService{ @TransactionAttribute(TransactionAttributeType.MANDATORY) public void deliver(Order order) { order.setDelivered(true); order.setProductId(2); } }
Since the transaction should be still running, I expect that order is still in the persistence context and that the changes made by the DeliveryService should be automatically persisted. However they never arrive in the database.
Do I get the whole concept or persistence context wrong? Or is there some bug in my code?
<?xml version="1.0" encoding="UTF-8"?> <!-- $Id: mysql-ds.xml 71535 2008-04-01 07:05:03Z adrian@jboss.org $ --> <!-- Datasource config for MySQL using 3.0.9 available from: http://www.mysql.com/downloads/api-jdbc-stable.html --> <datasources> <local-tx-datasource> <jndi-name>ECBDS</jndi-name> <connection-url>jdbc:mysql://localhost:3306/ecb</connection-url> <driver-class>com.mysql.jdbc.Driver</driver-class> <user-name>root</user-name> <password>ritak1</password> <exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name> <!-- should only be used on drivers after 3.22.1 with "ping" support <valid-connection-checker-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLValidConnectionChecker</valid-connection-checker-class-name> --> <!-- sql to call when connection is created <new-connection-sql>some arbitrary sql</new-connection-sql> --> <!-- sql to call on an existing pooled connection when it is obtained from pool - MySQLValidConnectionChecker is preferred for newer drivers <check-valid-connection-sql>some arbitrary sql</check-valid-connection-sql> --> <!-- corresponding type-mapping in the standardjbosscmp-jdbc.xml (optional) --> <metadata> <type-mapping>mySQL</type-mapping> </metadata> </local-tx-datasource> </datasources>
<?xml version="1.0" encoding="UTF-8"?> <persistence:persistence version="1.0" xmlns:persistence="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 persistence_1_0.xsd"> <persistence:persistence-unit name="ECB"> <persistence:description>ECB</persistence:description> <persistence:jta-data-source>java:ECBDS</persistence:jta-data-source> <persistence:properties> <persistence:property name="hibernate.hbm2ddl.auto" value="create-drop" /> <persistence:property name="dialect" value="org.hibernate.dialect.MySQLDialect" /> <persistence:property name="hibernate.show_sql" value="true" /> <persistence:property name="hibernate.format_sql" value="true" /> </persistence:properties> </persistence:persistence-unit> </persistence:persistence>