EJB方法抛异常,事务怎么没有回滚
EJB中的方法,如果ID传“111111”方法会抛出CreateException,EJB部署的是:
<container-transaction>
<method>
<ejb-name>PdUserSession</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
但抛异常后,数据还是插到数据库中了,为什么不回滚!!
public void createUser(String id, String pass, String first, String last, String group) throws NamingException, CreateException {
DataSource ds = (DataSource) EjbHomeFactory.lookup("DataSource");
try {
Connection con = ds.getConnection();
PreparedStatement ps = con.prepareStatement(
"insert into pd_user(id) values ('" + id + "')");
ps.executeUpdate();
}
catch (SQLException ex) {
ex.printStackTrace();
}
if(id.indexOf("1") > -1){
throw new CreateException();
}
}
如果改成BEAN管理事务的,就可以了,难道上面的那个事务没起来
public void createUser(String id, String pass, String first, String last, String group) throws NamingException, CreateException {
/**@todo Complete this method*/
UserTransaction tx = sessionContext.getUserTransaction();
try {
System.out.println("===========begin============");
tx.begin();
/*
String description = "Demo Description";
String rgyName = "cn=" + id + "," + PdHelper.rgySuffix;
PDRgyUserName pdRgyUserName =
new PDRgyUserName(rgyName, first, last);
boolean ssoUser = false;
boolean pwdPolicy = true;
ArrayList groupList = new ArrayList();
groupList.add(group);
System.out.println("Creating a user...\n");
PDUser.createUser(ContextFactory.getContext(),
id,
pdRgyUserName,
description,
pass.toCharArray(),
groupList,
ssoUser,
pwdPolicy,
new PDMessages());
*/
DataSource ds = (DataSource) EjbHomeFactory.lookup("DataSource");
Connection con = ds.getConnection();
PreparedStatement ps = con.prepareStatement(
"insert into pd_user(id) values ('" + id + "')");
ps.executeUpdate();
//PdUserHome home = (PdUserHome) EjbHomeFactory.lookup("PdUser");
//PdUser user = home.create(id, pass, first, last, group);
}
catch (Exception ex1) {
ex1.printStackTrace();
}
finally{
System.out.println("fianlly======================");
try {
if(id.indexOf("1") > -1){
System.out.println("rollback======================");
tx.rollback();
}else{
tx.commit();
}
}
catch (Exception ex) {
ex.printStackTrace();
}
}
}
还是我理解的有问题