当前位置: 技术问答>java相关
java中关于事务处理的问题,急需解答!在线等待!
来源: 互联网 发布时间:2015-04-16
本文导语: public class TestBean implements SessionBean { private SessionContext sessionContext; private Context ctx = null; private DataSource ds = null; public TestBean(){} public void ejbCreate()throws javax.ejb.CreateException { Properties p= new...
public class TestBean implements SessionBean {
private SessionContext sessionContext;
private Context ctx = null;
private DataSource ds = null;
public TestBean(){}
public void ejbCreate()throws javax.ejb.CreateException {
Properties p= new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.T3InitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://192.168.1.210:7001");
try{
javax.naming.Context ctx = (javax.naming.Context) new javax.naming.InitialContext(p);
ds = (javax.sql.DataSource)ctx.lookup("apache");
} catch (Exception e) {
System.out.println(e.getMessage());
throw new CreateException();
}
}
public RowSet getApache(String sql)throws SQLException{
Connection con = null;
ResultSet rs;
CachedRowSet crs;
try{
crs = new CachedRowSet();
con = ds.getConnection();
Statement stmt = con.createStatement();
rs = stmt.executeQuery(sql);
crs.populate(rs);
rs.close();
stmt.close();
} finally{
if (con != null){
System.out.println("not ok");
con.close();
}}
return crs;
}
public String updateApache(RowSet rs) throws SQLException {
Connection con = null;
try{
CachedRowSet crs = (CachedRowSet)rs;
con = ds.getConnection();
crs.acceptChanges(con);
} finally{
if (con != null)
{
System.out.println("ok");
con.close();
}
}
return "success";
}
public String commit(RowSet rs,RowSet rs1,RowSet rs2) throws SQLException {
Connection con = null;
try{
CachedRowSet crs = (CachedRowSet)rs;
CachedRowSet crs1 = (CachedRowSet)rs1;
CachedRowSet crs2 = (CachedRowSet)rs2;
con = ds.getConnection();
con.setAutoCommit(false);
crs.acceptChanges(con);
crs1.acceptChanges(con);
crs2.acceptChanges(con);
con.commit();
}catch(SQLException sql){
con.rollback();
}
finally{
if (con != null)
{
con.close();
}
}
return "success";
}
public void ejbRemove() throws RemoteException {
}
public void ejbActivate() throws RemoteException {
}
public void ejbPassivate() throws RemoteException {
}
public void setSessionContext(SessionContext sessionContext) throws RemoteException {
this.sessionContext = sessionContext;
}
}
为什么在 commit中crs1,crs2操作失败的情况下crs为什么不rollback?怎么才能实现呢?
private SessionContext sessionContext;
private Context ctx = null;
private DataSource ds = null;
public TestBean(){}
public void ejbCreate()throws javax.ejb.CreateException {
Properties p= new Properties();
p.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.T3InitialContextFactory");
p.put(Context.PROVIDER_URL,"t3://192.168.1.210:7001");
try{
javax.naming.Context ctx = (javax.naming.Context) new javax.naming.InitialContext(p);
ds = (javax.sql.DataSource)ctx.lookup("apache");
} catch (Exception e) {
System.out.println(e.getMessage());
throw new CreateException();
}
}
public RowSet getApache(String sql)throws SQLException{
Connection con = null;
ResultSet rs;
CachedRowSet crs;
try{
crs = new CachedRowSet();
con = ds.getConnection();
Statement stmt = con.createStatement();
rs = stmt.executeQuery(sql);
crs.populate(rs);
rs.close();
stmt.close();
} finally{
if (con != null){
System.out.println("not ok");
con.close();
}}
return crs;
}
public String updateApache(RowSet rs) throws SQLException {
Connection con = null;
try{
CachedRowSet crs = (CachedRowSet)rs;
con = ds.getConnection();
crs.acceptChanges(con);
} finally{
if (con != null)
{
System.out.println("ok");
con.close();
}
}
return "success";
}
public String commit(RowSet rs,RowSet rs1,RowSet rs2) throws SQLException {
Connection con = null;
try{
CachedRowSet crs = (CachedRowSet)rs;
CachedRowSet crs1 = (CachedRowSet)rs1;
CachedRowSet crs2 = (CachedRowSet)rs2;
con = ds.getConnection();
con.setAutoCommit(false);
crs.acceptChanges(con);
crs1.acceptChanges(con);
crs2.acceptChanges(con);
con.commit();
}catch(SQLException sql){
con.rollback();
}
finally{
if (con != null)
{
con.close();
}
}
return "success";
}
public void ejbRemove() throws RemoteException {
}
public void ejbActivate() throws RemoteException {
}
public void ejbPassivate() throws RemoteException {
}
public void setSessionContext(SessionContext sessionContext) throws RemoteException {
this.sessionContext = sessionContext;
}
}
为什么在 commit中crs1,crs2操作失败的情况下crs为什么不rollback?怎么才能实现呢?
|
可能是SQL已自动提交
加上
con.setAutoCommit(false);
设置为手动提交
加上
con.setAutoCommit(false);
设置为手动提交