I'm building an app with Java that storage the datas into Mysql database.
So I'm building the code that first of save the data, setAutocommit=false, if the datas are correctly save on database setAutocommit=true.
If there is an error execute a rollback of data.
So the code is execute never error but not works because, the data that I try to save have a Primary Key Integer autoincrement and if there is an error at the next save, the system jump the number.
For example in my database the filed CodiceFattura = 100, if I try to save another record CodiceFattura = 101. Now If I have CodiceFattura = 100, I try to save, I have an error, I fixed the error then save CodiceFattura = 102 and not 101.
This is the code:
public boolean salvaFattura(String tipoFattura){
fatturaIstanza= istanziaValori(null);
boolean tuttoOk=false;
try{
setCommitManager(false);
tuttoOk=modelManager.getFatturaManager().inserisciFatturaArticoliSenzaCodice(fatturaIstanza,tipoFattura);
if(!tuttoOk){
VisualMessageFattura.getErrore();
eseguiRollBack();
}
Integer.parseInt(null);
setCommitManager(true);
}
catch(Exception e){
VisualMessage.getShowMessaggioErroreFatturaSalvata("Fattura");
log.logStackTrace(e);
eseguiRollBanck();
return false;
}
return tuttoOk;
}
public void eseguiRollBanck(){
try{
modelManager.getFatturaManager().eseguiRollBack();
}catch(Exception e){
log.logStackTrace(e);
}
try{
db.eseguiRollBack();
}catch(Exception e){
log.logStackTrace(e);
}
}
public void setCommitManager(Boolean state){
modelManager.getFatturaManager().setAutoCommit(state);
}
public void eseguiRollBack(){
try {
db.rollback();
} catch (SQLException e) {
log.logStackTrace(e);
//VisualMessage.getErroreDB();
}
}
public void setAutoCommit(boolean autoCommit){
try {
db.setAutoCommit(autoCommit);
} catch (SQLException e) {
log.logStackTrace(e);
VisualMessage.getErroreDB();
}
}