1、首先在jboss 中mysql-ds.xml的配置文件中配置二个数据源
2、在建立好的ejb项目中的persistence.xml配置文件中做好相应的配置
3、具体编码
实验结果:在add方法中如果不抛出异常,那么二个数据库中的数据都能添加成功,而如果在在二个方法中有一个出现错误,事物便会回滚保证事物的一致性。
<local-tx-datasource>
<jndi-name>MySqlDS</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/ejb</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>admin</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
<local-tx-datasource>
<jndi-name>MySqlDS2</jndi-name>
<connection-url>jdbc:mysql://localhost:3306/ejb2</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<user-name>root</user-name>
<password>admin</password>
<exception-sorter-class-name>org.jboss.resource.adapter.jdbc.vendor.MySQLExceptionSorter</exception-sorter-class-name>
<metadata>
<type-mapping>mySQL</type-mapping>
</metadata>
</local-tx-datasource>
2、在建立好的ejb项目中的persistence.xml配置文件中做好相应的配置
<persistence-unit name="test" transaction-type="JTA">
<jta-data-source>java:/MySqlDS</jta-data-source>
<class>com.bjsxt.jpa.Person</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
<persistence-unit name="test2" transaction-type="JTA">
<jta-data-source>java:/MySqlDS2</jta-data-source>
<class>com.bjsxt.jpa.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
<property name="hibernate.show_sql" value="true"/>
<property name="hibernate.hbm2ddl.auto" value="update"/>
</properties>
</persistence-unit>
3、具体编码
//为了测试不同的事物,下面编写了2个实体类
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Person implements Serializable{
@Id
@GeneratedValue
private int id;
private String name;
public int getId() {
return id;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
}
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class User implements Serializable{
@Id
@GeneratedValue
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
//编写接口
public interface PersonService {
public void add(Person p, User u);
}
//编写实现
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
@Stateless
@Remote
@Local
public class PersonServiceImpl implements PersonService {
//注意:下面的add方法中存储的二个对象是放在两个不同的数据库中的
@PersistenceContext(unitName="test")
private EntityManager entityManager;
@PersistenceContext(unitName="test2")
private EntityManager entityManager2;
public void add(Person p, User u) {
entityManager.persist(p);
entityManager2.persist(u);
//在此抛出一个异常,看事物能否回滚、
throw new RuntimeException("Error"); }
}
//客户端调用
import javax.naming.InitialContext;
import com.bjsxt.jpa.Person;
import com.bjsxt.jpa.PersonService;
import com.bjsxt.jpa.User;
public class Client {
public static void main(String[] args) throws Exception{
InitialContext ctx = new InitialContext();
PersonService ps = (PersonService)ctx.lookup("PersonServiceImpl/remote");
Person p = new Person();
p.setName("张三");
User u = new User();
u.setName("李四");
ps.add(p, u);
}
}
实验结果:在add方法中如果不抛出异常,那么二个数据库中的数据都能添加成功,而如果在在二个方法中有一个出现错误,事物便会回滚保证事物的一致性。