容器管理事务怎样支持多数据源
我采用的容器管理事务,em和emM是两个不同的数据源,不过已经写在xa的配置里了。我想让容器来管理分布式的事务,可以吗?该怎么做?
以下是我写的例子,执行到emM那边就不行了。
@TransactionManagement(TransactionManagementType.CONTAINER)
public class ClientAuditSessionBeanImpl extends BaseSessionBeanImpl implements
IClientAuditSessionBeanRemote, IClientAuditSessionBeanLocal
{
@PersistenceContext(unitName = "PostgresDS")
protected EntityManager em;
@PersistenceContext(unitName = "MySqlDSDuan")
protected EntityManager emM;
/**
* 客户端鉴权
*
* @param clientAuthenticationREQ 客户端参数bean
*/
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public ResponseBean clientAudit(
ClientAuthenticationREQ clientAuthenticationREQ)
{
try
{
em.createQuery();//em的操作
dd();
String dd = "dd";
Integer.parseInt(dd);
}
catch (Exception e)
{
logger.error("[" + this.getClass().getSimpleName() + "] "
+ e.getMessage(), e);
/**
* 为了使容器能获取到异常 从而自动回滚
*/
throw new RuntimeException();
}
return rb;
}
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public void dd()
{
Query d = emM
.createNativeQuery(" insert into tbl_dish (dish_name,dish_price) values ('ddddd','dddd') ");
d.executeUpdate();
}
}
这个代码执行到d.executeUpdate();就报错了,不知道问题在哪里
<datasources>
<xa-datasource>
<jndi-name>PostgresDS</jndi-name>
<xa-datasource-class>org.postgresql.xa.PGXADataSource</xa-datasource-class>
<xa-datasource-property name="serverName">172.20.4.23</xa-datasource-property>
<xa-datasource-property name="portNumber">5432</xa-datasource-property>
<xa-datasource-property name="databaseName">gaea</xa-datasource-property>
<xa-datasource-property name="user">postgres</xa-datasource-property>
<xa-datasource-property name="password">postgres</xa-datasource-property>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.PostgreSQLValidConnectionChecker</exception-sorter-class-name>
<no-tx-separate-pools/>
<metadata>
<type-mapping>PostgreSQL 8.4</type-mapping>
</metadata>
</xa-datasource>
<xa-datasource>
<jndi-name>MySqlDSDuan</jndi-name>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
<xa-datasource-property name="Url">jdbc:mysql://172.20.4.23:3306/dish</xa-datasource-property>
<xa-datasource-property name="User">root</xa-datasource-property>
<xa-datasource-property name="Password">111111</xa-datasource-property>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<no-tx-separate-pools/>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</xa-datasource>
</datasources>
请大家帮忙找下原因,谢谢