我想在一个事务中执行多个语句。我创建了一个连接对象,并将autocommit设置为false;
例如,我有五张表需要在一次交易中进行更新;我为每个类制作了5个类,这是自己的更新方法。我曾经用一种方法来处理所有这些更新语句,但是我的老师告诉我这是一种不好的编码方式。我应该把它们放在不同的班级。
public boolean updateBusinessRule(BusinessRule businessRule){
try (Connection con = super.getConnectionConfigDb()) {
con.setAutoCommit(false);
// Update table BusinessRule
if (businessRule.getOperatorID() != 0) {
String queryBr = "UPDATE BUSINESSRULE SET ERRORMESSAGE = ?, SQLCODE = ?, CUSTOMNAME = ?, OPERATOR_ID = ? WHERE BUSINESSRULE_ID = ?";
try (PreparedStatement pstmtBr = con.prepareStatement(queryBr)) {
pstmtBr.setString(1, businessRule.getErrorMessage());
pstmtBr.setString(2, businessRule.getSqlQuery());
pstmtBr.setString(3, businessRule.getName());
pstmtBr.setInt(4, businessRule.getOperatorID());
pstmtBr.setInt(5, businessRule.getBusinessRuleID());
pstmtBr.executeUpdate();
} catch (SQLException e) {
con.rollback();
e.printStackTrace();
return false;
}
} else {
String queryBr = "UPDATE BUSINESSRULE SET ERRORMESSAGE = ?, SQLCODE = ?, CUSTOMNAME = ? WHERE BUSINESSRULE_ID = ?";
try (PreparedStatement pstmtBr = con.prepareStatement(queryBr)) {
pstmtBr.setString(1, businessRule.getErrorMessage());
pstmtBr.setString(2, businessRule.getSqlQuery());
pstmtBr.setString(3, businessRule.getName());
pstmtBr.setInt(4, businessRule.getBusinessRuleID());
pstmtBr.executeUpdate();
} catch (SQLException e) {
con.rollback();
e.printStackTrace();
return false;
}
}
// Update table TargetTable
String queryTt = "UPDATE TARGETTABLE SET NAME = ? WHERE TABLE_ID = ?";
for (int i = 0 ; i < businessRule.getTableListSize() ; i++) {
try (PreparedStatement pstmtTt = con.prepareStatement(queryTt)) {
pstmtTt.setString(1,businessRule.getListOfTables().get(i).getName());
pstmtTt.setInt(2, businessRule.getListOfTables().get(i).getId());
pstmtTt.executeUpdate();
} catch (SQLException e) {
con.rollback();
e.printStackTrace();
return false;
}
}
// Update table Attribute
String queryAt = "UPDATE ATTRIBUTE SET ATTRIBUTENAME = ?, ATTRIBUTETYPE = ?, DATALENGTH = ? WHERE ATTRIBUTE_ID = ?";
System.out.println(businessRule.getListOfColumns().size());
for (int i = 0 ; i < businessRule.getColumnListSize() ; i++) {
try (PreparedStatement pstmtAt = con.prepareStatement(queryAt)) {
pstmtAt.setString(1,businessRule.getListOfColumns().get(i).getName());
pstmtAt.setString(2,businessRule.getListOfColumns().get(i).getDataType());
pstmtAt.setInt(3,businessRule.getListOfColumns().get(i).getDataLength());
pstmtAt.setInt(4,businessRule.getListOfColumns().get(i).getId());
pstmtAt.executeUpdate();
} catch (SQLException e) {
con.rollback();
e.printStackTrace();
return false;
}
}
// Update table Value
String queryVa = "UPDATE VALUE SET VALUE = ? WHERE VALUE_ID = ?";
for (int i = 0 ; i < businessRule.getListOfValues().size() ; i++) {
try (PreparedStatement pstmtVa = con.prepareStatement(queryVa)) {
pstmtVa.setInt(1,businessRule.getListOfValues().get(i).getValue());
pstmtVa.setInt(2,businessRule.getListOfValues().get(i).getId());
pstmtVa.executeUpdate();
} catch (SQLException e) {
con.rollback();
e.printStackTrace();
return false;
}
}
// Update table Stack
String querySt = "UPDATE BUSINESSRULESSTACK SET NAME = ? WHERE BUSINESSRULE_ID = ?";
try (PreparedStatement pstmtSt = con.prepareStatement(querySt)) {
pstmtSt.setString(1,businessRule.getName());
pstmtSt.setInt(2,businessRule.getBusinessRuleID());
pstmtSt.executeUpdate();
} catch ( SQLException e) {
con.rollback();
e.printStackTrace();
return false;
}
/*
When it reaches this point it means everything went fine so next step is commit and return true
*/
con.commit();
return true;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
在一个名为BusinessRuleDaoImpl.java的新类中,我在其中编写了以下函数。请注意,它正在调用另一个类中的另一个方法以使用相同的连接对象执行第二条语句。我一直坚持到上课为止。好吧,我感觉自己做的不好,还不知道它的好作法。谁能给我建议?谢谢。
@Override
public boolean updateBusinessRule(BusinessRule businessRule, Connection con) {
try (Connection conBR = con) {
if (businessRule.getOperatorID() != 0) {
String queryBr = "UPDATE BUSINESSRULE SET ERRORMESSAGE = ?, SQLCODE = ?, CUSTOMNAME = ?, OPERATOR_ID = ? WHERE BUSINESSRULE_ID = ?";
try (PreparedStatement pstmtBr = conBR.prepareStatement(queryBr)) {
pstmtBr.setString(1, businessRule.getErrorMessage());
pstmtBr.setString(2, businessRule.getSqlQuery());
pstmtBr.setString(3, businessRule.getName());
pstmtBr.setInt(4, businessRule.getOperatorID());
pstmtBr.setInt(5, businessRule.getBusinessRuleID());
pstmtBr.executeUpdate();
return tableDao.updateTable(businessRule,conBR);
} catch (SQLException e) {
con.rollback();
e.printStackTrace();
return false;
}
} else {
String queryBr = "UPDATE BUSINESSRULE SET ERRORMESSAGE = ?, SQLCODE = ?, CUSTOMNAME = ? WHERE BUSINESSRULE_ID = ?";
try (PreparedStatement pstmtBr = con.prepareStatement(queryBr)) {
pstmtBr.setString(1, businessRule.getErrorMessage());
pstmtBr.setString(2, businessRule.getSqlQuery());
pstmtBr.setString(3, businessRule.getName());
pstmtBr.setInt(4, businessRule.getBusinessRuleID());
pstmtBr.executeUpdate();
} catch (SQLException e) {
con.rollback();
e.printStackTrace();
return false;
}
}
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
答案 0 :(得分:0)
常见做法是为每个表/实体赋予其自己的DAO,服务和控制器。业务规则表在Java中具有其对象,BusinessRules Dao,BusinessRulesService和BusinessRulesController。价值,归因等也是如此。服务可以相互通信以一种方法更新所有5个表。刀道只应该与其服务和DTO对话。