Hello Everyone,
First of all I would like to thank the community to help me understand my problem. Also, Excuse me for my bad English, it's not my main language.
ProblemI am using
org.hibernate.Criteria to read records from database. Sometimes (in 1% of time), my method does not return all the records.
When the problem occurred, there is no Exception.
Code:
@SuppressWarnings("unchecked")
public List<InvoiceLine> getInvoiceLines(final Session aSession, final InvoiceId aId) {
long iStart = System.currentTimeMillis();
Criteria iCriteria = aSession.createCriteria(InvoiceLine.class);
iCriteria.add(Restrictions.eq("id.invoiceNumber", aId.getInvoiceNumber()));
iCriteria.add(Restrictions.eq("id.invoiceType", aId.getInvoiceType()));
iCriteria.add(Restrictions.eq("id.sellingType", aId.getSellingType()));
iCriteria.add(Restrictions.eq("id.proforma", aId.isProforma()));
List<InvoiceLine> iResults = iCriteria.list();
logger.debug(String.format("aSession<%s> InvoiceId<%s> iResults<%d> time: %d", aSession, aId, iResults.size(), System.currentTimeMillis() - iStart));
return iResults;
}
ProcessHere the process where I use this method:
Code:
public InvoiceLine addInvoiceLine(final Invoice aInvoice, final InvoiceLine aInvoiceLine) throws Exception {
long iStart = System.currentTimeMillis();
try {
// Save invoice_line
this.saveInvoiceLine(aInvoice, aInvoiceLine);
// Update Invoice's amount
List<InvoiceLine> iInvoiceLines = this.getInvoiceLines(aInvoice.getId());
this.updateInvoiceAmount(aInvoice, iInvoiceLines);
this.save(aInvoice);
logger.debug(String.format("Invoice<%s> InvoiceLine<%s> time: %d", aInvoice.getId(), aInvoiceLine.getId(), System.currentTimeMillis() - iStart));
return aInvoiceLine;
} catch (Exception aException) {
logger.error(String.format("Invoice<%s> InvoiceLine<%s> time: %d", aInvoice.getId(), aInvoiceLine.getId(), System.currentTimeMillis() - iStart), aException);
throw aException;
}
}
ExplanationFirst, I execute
this.saveInvoiceLine(aInvoice, aInvoiceLine) that insert/update the invoice_line in database. To perform the insert/update, I use
getSessionFactory().openSession() to create my
org.hibernate.Session and execute my insert/update in a transaction. The transaction is then committed and the
org.hibernate.Session is closed.
And, I execute
this.getInvoiceLines(aInvoice.getId()) to read all invoice_line associated with my invoice.
The just added invoice_line is not returned.
TheoryOur Team believe that we have a flush problem sometimes. We believe that Hibernate have some sort of "cache" and when the
this.getInvoiceLines() is executed, the cache simply does not have the added invoice_line.
Thank you for your help.