请查看以下代码
DBHandler.java
@Override
public String insertClient(String name, String address, String phone, String email, String country, Date visaGrantedDate, Date visaEntryDate)
{
int i=0;
try
{
con.setAutoCommit(false);
PreparedStatement ps = con.prepareStatement("insert into Client (Name, Address, Phone, Email, Country, Visa_Granted_Date, Visa_Entry_Date) values (?,?,?,?,?,?,?)");
ps.setString(1,name);
ps.setString(2,address);
ps.setString(3,phone);
ps.setString(4, email);
ps.setString(5, country);
ps.setDate(6, visaGrantedDate);
ps.setDate(7, visaEntryDate);
i = ps.executeUpdate();
con.commit();
if(i>0)
{
return "Data saved successfully";
}
else
{
return "Error in saving data";
}
}
catch(SQLException e)
{
try
{
con.rollback();
e.printStackTrace();
return "Failed to save data. Data safely rolled back";
}
catch(Exception ee)
{
ee.printStackTrace();
return "Failed to save data. Data roll back failed";
}
}
}
@Override
public String insertPortfolio(String portfolioID, String clientName, double amount, double dicn, String lawyer, String introducer, String provider, String Types)
{
int i=0;
try
{
con.setAutoCommit(false);
PreparedStatement ps = con.prepareStatement("insert into Portfolio (portfolio_id, Client_Name, Amount, riteria_Net, Lawyer, Introducer, Provider, Types) values (?,?,?,?,?,?,?,?)");
ps.setString(1,portfolioID);
ps.setString(2,clientName);
ps.setDouble(3,amount);
ps.setDouble(4, dicn);
ps.setString(5, lawyer);
ps.setString(6, introducer);
ps.setString(7, provider);
ps.setString(8, Types);
i = ps.executeUpdate();
con.commit();
if(i>0)
{
return "Data saved successfully";
}
else
{
return "Error in saving data";
}
}
catch(SQLException e)
{
try
{
con.rollback();
e.printStackTrace();
return "Failed to save data. Data safely rolled back";
}
catch(Exception ee)
{
ee.printStackTrace();
return "Failed to save data. Data roll back failed";
}
}
}
@Override
public String insertClientFees(String portfolioID, double initialFees, double initialStepnaFees, double initialIntraFees, double ongoingFees, double ongoingStepnaFees, double ongoingIntraFees, double ongoingFabulaFees, double Fees, double otherFees, double VAT)
{
int i=0;
try
{
con.setAutoCommit(false);
PreparedStatement ps = con.prepareStatement("insert into Client_Fees (portfolio_id, Initial_Fees, InitiaFees2, Initial_intra_Fees, Ongoing_Fees, Ongoing_octa_Fees, Ongoing_intra_Fees, Ongoing_fabula_Fees, Ongoing_stepna_Fees, Other, VAT) values (?,?,?,?,?,?,?,?,?,?,?)");
ps.setString(1,portfolioID);
ps.setDouble(2,initialintraFees);
//Rest of the data insert code has been removed
i = ps.executeUpdate();
con.commit();
if(i>0)
{
return "Data saved successfully";
}
else
{
return "Error in saving data";
}
}
catch(SQLException e)
{
try
{
con.rollback();
e.printStackTrace();
return "Failed to save data. Data safely rolled back";
}
catch(Exception ee)
{
ee.printStackTrace();
return "Failed to save data. Data roll back failed";
}
}
}
您在上面的代码中看到的是如何将数据插入同一数据库中的3个不同表中。但是,"表单字段"因为所有这三个表都包含在一个表单中,所以所有这些表都应该立即更新,如果发生某些事情,应该回滚插入所有3个表的数据。例如,下面是如何调用数据库代码。
MainUI.java
dbConnector.insertClient(name, address, phone, email, country, null, null);
dbConnector.insertPortfolio(clientPortfolioId, name, amount, dicn, name, country, phone, visaEntryDate);
dbConnector.insertClientFees(clientPortfolioId, initialFees, initialVisionFees, initialintraFees, ongoingFees, ongoingStepnaFees, ongoingIntraFees, ongoingFabulaFees, ongoingserFees, otherFees, VAT)
所以我的问题是,如果出现问题,我怎样才能将刚刚插入所有3个表的数据回滚到一起?
答案 0 :(得分:5)
你应该简单地从他们之外的方法推出try...catch
块:
try {
con.setAutoCommit(false);
dbConnector.insertClient(...
dbConnector.insertPortfolio(...
dbConnector.insertClientFees(...
con.commit();
} catch(SQLException e) {
try {
con.rollback();
e.printStackTrace();
return "Failed to save data. Data safely rolled back";
}
...
}
答案 1 :(得分:2)
您目前有3个交易,每个方法一个。您需要进行一次交易(一次setAutoCommit(false)
,一次commit()
或rollback()
。
因此,您需要相应地重构代码。
答案 2 :(得分:1)
你的con.setAutoCommit(false);很好,但你可能不会做con.commit();在每三个功能完成之前......
try {
dbConnector.disableAutoCommit();
dbConnector.insertClient(name, address, phone, email, country, null, null);
dbConnector.insertPortfolio(clientPortfolioId, name, amountInvested, dicn, name, country, phone, visaEntryDate);
dbConnector.insertClientFees(clientPortfolioId, initialEdenhurstFees, initialVisionFees, initialIntroducerFees, ongoingEdenhurstFees, ongoingVisionFees, ongoingIntroducerFees, ongoingCastleFees, ongoingEdenCastleFees, otherFees, VAT)
dbConnector.commit();
} catch(Esxception e) {
dbConnector.rollback();
}
更像这样,你插入函数不应该执行setAutocommit(false)和commit(),rollback()的东西,也不要自己捕获异常