In a project that uses NinjaFrameWork we are having a problem when querying the data with successive or concurrent requests.
In some cases it is receiving some data, and in other cases it is receiving other data. It seems as if the frameWork cache is kicking in at times.
We insert data into the database through a request style:
@Transactional
public Result askCita(Context context, DataCitasAsk cita) {
manager.getTransaction().begin();
manager.persist(cita);
manager.getTransaction().commit();
}
And when we collect data from the database through a request like this; but it does not always return the same data. Sometimes it does not return the last data inserted.
@Transactional
public Result listCitas(Context context, DataCitasList cita) {
if (cita == null) {
return Results.json().render(false);
}
// La IP del cliente
String ipClient = context.getRemoteAddr();
EntityManager entityManager = provider.get();
DbCitas db = DbCitas.getInstance();
DataCitasListResult vUser = db.listCitas(entityManager, cita);
return Results.json().render(vUser);
}
public DataCitasListResult listCitas(EntityManager manager,
DataCitasList data) {
String uCheck = Cita.QUERY_CLINIC_MONTH;
Calendar calBefore = Calendar.getInstance();
Calendar calAfter = Calendar.getInstance();
calAfter.set(Calendar.MONTH, calAfter.get(Calendar.MONTH) + 1);
DatabaseController db = DatabaseController.getInstance(manager);
Query q = db.getQuery(uCheck);
q.setParameter("dateBegan", calBefore.getTime());
q.setParameter("dateEnd", calAfter.getTime());
q.setParameter("idClinic", data.getIdClinic());
List<Cita> list = q.getResultList();
DbVetData vData = DbVetData.getInstance();
VetData clinic = vData.getClinicDb(manager, data.getIdClinic());
DataCitasListResult rData = new DataCitasListResult();
rData.setClinica(clinic);
rData.setCitas(new ArrayList<DataCitasResultItem>());
if (list.isEmpty()) {
return rData;
}
ArrayList<DataCitasResultItem> lCitas = new ArrayList<DataCitasResultItem>();
for (Cita cita : list) {
DataCitasResultItem nCita = new DataCitasResultItem();
nCita.setAnswered(cita.getDateVet() != null ? true : false);
nCita.setCreated(spf.format(cita.getCreated().getTime()));
if (cita.getDateCita() != null) {
nCita.setDateCita(spf.format(cita.getDateCita().getTime()));
}
if (cita.getDateVet() != null) {
nCita.setDateVet(spf.format(cita.getDateVet().getTime()));
}
nCita.setId(cita.getId());
nCita.setIdMascota(cita.getPet().getId());
nCita.setNameMascota(cita.getPet().getName());
nCita.setMotivo(cita.getMotive());
nCita.setTipoCita(cita.getType().getId());
//
lCitas.add(nCita);
}
rData.setCitas(lCitas);
return rData;
}
In the database if they appear; it seems little more than consultation, insertion.
I do not understand why.
PD: I include the Citas.class definition:
@Entity(name = "citas")
@Table(name = "citas")
public class Cita {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Column(name = "created")
private Timestamp created;
@ManyToOne
@JoinColumn(name = "id_mascota")
private Pet pet;
@Column(name = "date_cita")
private Calendar dateCita;
@Column(name = "date_vet")
private Calendar dateVet;
@Column(name = "motivo")
private String motive;
@Column(name = "sended")
private long sended;
@ManyToOne
@JoinColumn(name = "tipo_cita")
private CitaType type;
@ManyToOne
@JoinColumn(name = "id_clinica")
private VetData clinica;
}