2
I’m developing a java application that uses Hibernate to do the mapping. I created the DAO’s of the classes, but when doing Insert, update and delete operations it returns me this Mysql error.
Follow my classes (I will post only the insertion method so it doesn’t get so long):
Masterdao.java - Mother class of all the other daos, to make creation easier.
public class MasterDAO {
public Session getSession(){
return HibernateUtil.getSessionFactory().openSession();
}
public void inserirObjeto(Object obj){
Session s = getSession();
s.beginTransaction();
s.save(obj);
s.getTransaction().commit();
s.close();
}
}
Alunodao.java - Dao of the student who extends the Masterdao:
public class AlunoDAO extends MasterDAO{
public void inserirAluno(Aluno aluno){
inserirObjeto(aluno);
}
}
Insert data.java - Class with main method for inserting objects in the database
public class InsereDados {
public static void main(String[] args) {
Aluno aluno = new Aluno();
aluno.setNome("João Neto");
aluno.setCpf(777555);
aluno.setDataNascimento(new Date(new String("09/05/1995")));
aluno.setMatricula(2012030);
aluno.setRg(123456);
AlunoDAO alunoDAO = new AlunoDAO();
alunoDAO.inserirAluno(aluno);
}
Running this generates the error:
Lock Wait timeout exceeded; Try restarting transaction
The funny thing is that sometimes it works, sometimes it goes normal, sometimes not. How to solve this problem?
This has deadlock face. You can demonstrate how it is that
Aluno
relates to other entities? Also goes the basic question, if you send to the bank, from within the application, a simple SQL made in the hand from outside of Hibernate such asselect * from aluno where 1=0
, it works?– Victor Stafusa
Isn’t there some ghost connections in the database locking the entire table? I’ve seen it happen, and then you have to tear down those connections by force.
– Victor Stafusa
Victor, at the moment I’m not at home, but as soon as I get home I put the relationship code, and as for running a simple code, it also hangs inside the Mysql Workbench, except select, which works both with Hibernate and without.
– João Neto
If you lock inside Mysql Workbench, this is a strong indication that the problem isn’t java or Hibernate, it’s something in Mysql.
– Victor Stafusa
Running the show processlist command, it shows some processes like Sleep. I don’t know if this has anything to do.
– João Neto
But it always worked normal, so it can’t be some property of the tables I generated with Hibernate?
– João Neto
:) I think you found the problem. Probably these processes are ghost connections. If you can kill them, see if Hibernate goes back to normal.
– Victor Stafusa
Precisely these processes with Sleep are in the bank that I am manipulating with Hibernate.
– João Neto