How do I use auto-increment keys and CMP beans with XA trans
johncook59 Jan 21, 2007 7:23 PMI use auto-increment keys for CMP EJBs. I am trying to port all our code from a local-tx-datasource to an xa-datasource. The problem is that the auto key generation is returning a null generated primary key, which causes the insert transaction to rollback. Here is an extract from the JBoss log:
00:21:48,656 INFO [RMI TCP Connection(6)-192.168.0.4] [STDOUT] Mon Jan 22 00:21:48 GMT 2007 DEBUG: Executing XA statement: XA START 0x4275737465722f3132,0x31,0x101
00:21:48,656 ERROR [RMI TCP Connection(6)-192.168.0.4] [CustomerManagerBean] Failed to create a customer: Primary key for created instance is null.
00:21:48,656 INFO [RMI TCP Connection(6)-192.168.0.4] [STDOUT] Mon Jan 22 00:21:48 GMT 2007 DEBUG: Executing XA statement: XA END 0x4275737465722f3132,0x31,0x101
00:21:48,656 INFO [RMI TCP Connection(6)-192.168.0.4] [STDOUT] Mon Jan 22 00:21:48 GMT 2007 DEBUG: Executing XA statement: XA ROLLBACK 0x4275737465722f3132,0x31,0x101
When I manually set the primary key, the insert completes as normal. This code worked perfectly with the non-XA datasource.
How do I use auto-increment keys with XA transactions?
The jbosscmp-jdbc.xml (generated from xdoclet) looks like this:
<ejb-name>Customer</ejb-name>
java:/BankDS
<datasource-mapping>mySQL</datasource-mapping>
<create-table>true</create-table>
<table-name>customer</table-name>
<cmp-field>
<field-name>id</field-name>
<column-name>customer_id</column-name>
<not-null/>
<jdbc-type>INTEGER</jdbc-type>
<sql-type>INTEGER</sql-type>
<auto-increment/>
</cmp-field>
<cmp-field>
<field-name>name</field-name>
<column-name>name</column-name>
</cmp-field>
<cmp-field>
<field-name>password</field-name>
<column-name>password</column-name>
</cmp-field>
<!-- jboss 3.2 features -->
<!-- optimistic locking does not express the exclusions needed -->
The datasource deployment descriptor is
<xa-datasource>
<jndi-name>BankDS</jndi-name>
<xa-datasource-class>
com.mysql.jdbc.jdbc2.optional.MysqlXADataSource
</xa-datasource-class>
<xa-datasource-property name="URL">
jdbc:mysql://localhost:3306/bank
</xa-datasource-property>
<user-name>dbuser</user-name>
dbuser
<transaction-isolation>
TRANSACTION_READ_COMMITTED
</transaction-isolation>
<max-pool-size>5</max-pool-size>
<min-pool-size>0</min-pool-size>
<blocking-timeout-millis>2000</blocking-timeout-millis>
<idle-timeout-minutes>2</idle-timeout-minutes>
<track-connection-by-tx>true</track-connection-by-tx>
<new-connection-sql>set autocommit=false</new-connection-sql>
<no-tx-separate-pools>true</no-tx-separate-pools>
<exception-sorter-class-name>
org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter
</exception-sorter-class-name>
<type-mapping>mySQL</type-mapping>
</xa-datasource>
The target database is MySQL 5.0.27 and the Connector/J v5.0.4.