看看下面的
UserTransaction ut = context.getUserTransaction(); try {
ut.begin();
...
Database operation
...
ut.commit();
} catch (Exception ex) {
try {
ut.rollback();
} catch (SystemException syex) {
throw new EJBException
("Rollback failed: " + syex.getMessage());
}
throw new EJBException
("Transaction failed: " + ex.getMessage());
}
}
UserTransaction ut = context.getUserTransaction(); try {
ut.begin();
...
Database operation
...
ut.commit();
} catch (Exception ex) {
try {
ut.rollback();
} catch (SystemException syex) {
throw new EJBException
("Rollback failed: " + syex.getMessage());
}
throw new EJBException
("Transaction failed: " + ex.getMessage());
}
}
怎么才能执行
UserTransaction ut = context.getUserTransaction();
我不知道你在jboss下是怎么得到
context.getUserTransaction();
是不是要想在jboss下使用usertransaction还需要配置什么?
String url = "localhost:1099";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "org.jnp.interfaces.NamingContextFactory");
properties.put(Context.PROVIDER_URL, url);
properties.put(Context.URL_PKG_PREFIXES, "org.jboss.naming:org.jnp.interfaces");
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS,
password == null ? "" : password);
} return new InitialContext(properties);
}
catch (Exception e) {
throw e;
}
}
private double machineBalance;
<<-------------------------
private SessionContext context;
------------------------->>
private Connection con;
private String dbName = "java:comp/env/jdbc/TellerDB"; public void withdrawCash(double amount) { UserTransaction ut = context.getUserTransaction(); try {
ut.begin();
updateChecking(amount);
machineBalance -= amount;
insertMachine(machineBalance);
ut.commit();
} catch (Exception ex) {
try {
ut.rollback();
} catch (SystemException syex) {
throw new EJBException
("Rollback failed: " + syex.getMessage());
}
throw new EJBException
("Transaction failed: " + ex.getMessage());
}
}
你的sessionbean中的SessionContext必须要有的,jboss和其它的服务器在这一点没有区别