LemonSnm 2012-10-24
ejb多数据库操作
2012-05-0718:06:18|分类:j2ee|字号订阅
<?xmlversion="1.0"encoding="UTF-8"?>
<!--$Id:mysql-ds.xml889482009-05-1514:09:08Zjesper.pedersen$-->
<!--DatasourceconfigforMySQLusing3.0.9availablefrom:
http://www.mysql.com/downloads/api-jdbc-stable.html
-->
<datasources>
<xa-datasource>
<jndi-name>mysqlds01</jndi-name>
<track-connection-by-tx>true</track-connection-by-tx>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
<xa-datasource-propertyname="ServerName">127.0.0.1</xa-datasource-property>
<xa-datasource-propertyname="DatabaseName">test01</xa-datasource-property>
<xa-datasource-propertyname="User">root</xa-datasource-property>
<xa-datasource-propertyname="Password">xinhua</xa-datasource-property>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</xa-datasource>
<xa-datasource>
<jndi-name>mysqlds02</jndi-name>
<track-connection-by-tx>true</track-connection-by-tx>
<xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
<xa-datasource-propertyname="ServerName">127.0.0.1</xa-datasource-property>
<xa-datasource-propertyname="DatabaseName">test02</xa-datasource-property>
<xa-datasource-propertyname="User">root</xa-datasource-property>
<xa-datasource-propertyname="Password">xinhua</xa-datasource-property>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</xa-datasource>
</datasources>
@Stateless(name="sm")
@Remote
publicclassStudentManagerBeanimplementsStudentManager{
@PersistenceContext(unitname="test01")
privateEntityManagerem;
@PersistenceContext(unitname="test02")
privateEntityManagerem1;
publicvoidaddStudent(Stringname){
Useru=newUser();
u.setName(name);
em.persist(u);
Personp=newPerson();
p.setName(name+"01");
em1.persist(p);
//thrownewRuntimeException("ohno!");
}
}
<?xmlversion="1.0"encoding="UTF-8"?>
<persistencexmlns="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
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unitname="test01"transaction-type="JTA">
<jta-data-source>java:/mysqlds01</jta-data-source>
<class>com.huazhen.jpa.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<propertyname="hibernate.hbm2ddl.auto"value="create"/>
<propertyname="hibernate.dialect"value="org.hibernate.dialect.MySQL5Dialect"/>
</properties>
</persistence-unit>
<persistence-unitname="test02"transaction-type="JTA">
<jta-data-source>java:/mysqlds02</jta-data-source>
<class>com.huazhen.jpa.Person</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<propertyname="hibernate.hbm2ddl.auto"value="create"/>
<propertyname="hibernate.dialect"value="org.hibernate.dialect.MySQL5Dialect"/>
</properties>
</persistence-unit>
</persistence>