Sleep operations timeout

Studying sleep mode .. please, easy :)

This is my main class, which I use to perform operations depending on the choice of users:

package hibernate_tut_emp; import java.util.Iterator; import java.util.List; import java.util.Scanner; import org.hibernate.Query; import org.hibernate.Session; import org.hibernate.SessionFactory; public class MyOps { public static void main(String[] args) { Scanner scnr = new Scanner(System.in); System.out.print(" 1 Insert \n 2 Delete \n 3 Update \n 4 Select \n Enter a choice : "); int choice = scnr.nextInt(); SessionFactory sesfact = HibernateUtil.getSessionFactory(); Session session = sesfact.openSession(); session.beginTransaction(); Employee emp = new Employee(); switch (choice) { case 1: //insert System.out.print("Enter a name : "); String name = scnr.next(); System.out.print("Enter a surname : "); String surname = scnr.next(); emp.setName(name); emp.setSurname(surname); session.save(emp); session.getTransaction().commit(); break; case 2://delete System.out.print("Enter a name to delete : "); String delname = scnr.next(); Query delQuery = session.getNamedQuery("DeleteByName"); delQuery.setParameter("name", delname); delQuery.executeUpdate(); session.getTransaction().commit(); break; case 3: //update System.out.print("Enter a name to update : "); String upname = scnr.next(); System.out.print("Enter a surname to update : "); String surn_name = scnr.next(); Query q = session.getNamedQuery("UpdateSurnameOfName"); q.setParameter("name", upname); q.setParameter("surname", surn_name); q.executeUpdate(); session.getTransaction().commit(); break; case 4: System.out.print("Enter a name to show details : "); String sh_name = scnr.next(); session = sesfact.getCurrentSession(); session.beginTransaction(); Query s_q = session.getNamedQuery("GetDetailsByName"); s_q.setParameter("name", sh_name); List list = s_q.list(); Iterator it = list.iterator(); while(it.hasNext()){ Employee employee = (Employee)it.next(); System.out.println(employee.getId()+" "+employee.getName()+" "+employee.getSurname()); } break; default: System.out.println("wrong choice maadi...."); break; } } } 

HibernateUtil.java

 package hibernate_tut_emp; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; public class HibernateUtil { private static final SessionFactory sessionFactory; private static ServiceRegistry serviceRegistry; static { try { Configuration configuration = new Configuration(); configuration.configure(); serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); sessionFactory = configuration.buildSessionFactory(serviceRegistry); } catch (Throwable th) { System.err.println("Initial SessionFactory creation failed" + th); throw new ExceptionInInitializerError(th); } } public static SessionFactory getSessionFactory() { return sessionFactory; } } 

Problem

So far, I have been executing all the queries individually, and they all worked as expected, but after I put them inside the switch , I get an Exception in thread "main" org.hibernate.exception.LockTimeoutException: could not execute statement . I assume this has something related to the connection pool.
Can someone please tell me exactly how it works here and how to release connections (if this is the proposed method) after the operation is completed?

Console:

  1 Insert 2 Delete 3 Update 4 Select Enter a choice : 1 Oct 03, 2014 11:50:27 AM org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit> INFO: HCANN000001: Hibernate Commons Annotations {4.0.5.Final} Oct 03, 2014 11:50:27 AM org.hibernate.Version logVersion INFO: HHH000412: Hibernate Core {4.3.6.Final} Oct 03, 2014 11:50:27 AM org.hibernate.cfg.Environment <clinit> INFO: HHH000206: hibernate.properties not found Oct 03, 2014 11:50:27 AM org.hibernate.cfg.Environment buildBytecodeProvider INFO: HHH000021: Bytecode provider name : javassist Oct 03, 2014 11:50:27 AM org.hibernate.cfg.Configuration configure INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml Oct 03, 2014 11:50:27 AM org.hibernate.cfg.Configuration getConfigurationInputStream INFO: HHH000040: Configuration resource: /hibernate.cfg.xml Oct 03, 2014 11:50:27 AM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.sourceforge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide! Oct 03, 2014 11:50:27 AM org.hibernate.cfg.Configuration doConfigure INFO: HHH000041: Configured SessionFactory: null Oct 03, 2014 11:50:27 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure WARN: HHH000402: Using Hibernate built-in connection pool (not for production use!) Oct 03, 2014 11:50:27 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000401: using driver [com.mysql.jdbc.Driver] at URL [jdbc:mysql://localhost:3306/hibernate] Oct 03, 2014 11:50:27 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000046: Connection properties: {user=root, password=****} Oct 03, 2014 11:50:27 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator INFO: HHH000006: Autocommit mode: false Oct 03, 2014 11:50:27 AM org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure INFO: HHH000115: Hibernate connection pool size: 1 (min=1) Oct 03, 2014 11:50:28 AM org.hibernate.dialect.Dialect <init> INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQLDialect Oct 03, 2014 11:50:28 AM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService INFO: HHH000399: Using default transaction strategy (direct JDBC transactions) Oct 03, 2014 11:50:28 AM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory <init> INFO: HHH000397: Using ASTQueryTranslatorFactory Oct 03, 2014 11:50:28 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000228: Running hbm2ddl schema update Oct 03, 2014 11:50:28 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000102: Fetching database metadata Oct 03, 2014 11:50:28 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000396: Updating schema Oct 03, 2014 11:50:28 AM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000261: Table found: hibernate.hib1 Oct 03, 2014 11:50:28 AM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000037: Columns: [id, name, surname] Oct 03, 2014 11:50:28 AM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000108: Foreign keys: [] Oct 03, 2014 11:50:28 AM org.hibernate.tool.hbm2ddl.TableMetadata <init> INFO: HHH000126: Indexes: [primary] Oct 03, 2014 11:50:28 AM org.hibernate.tool.hbm2ddl.SchemaUpdate execute INFO: HHH000232: Schema update complete Enter a name : mohd. Enter a surname : salim Hibernate: insert into hib1 (name, surname, id) values (?, ?, ?) Oct 03, 2014 11:51:27 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions WARN: SQL Error: 1205, SQLState: 41000 Oct 03, 2014 11:51:27 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions ERROR: Lock wait timeout exceeded; try restarting transaction Oct 03, 2014 11:51:27 AM org.hibernate.engine.jdbc.batch.internal.AbstractBatchImpl release INFO: HHH000010: On release of batch it still contained JDBC statements Exception in thread "main" org.hibernate.exception.LockTimeoutException: could not execute statement at org.hibernate.dialect.MySQLDialect$1.convert(MySQLDialect.java:447) at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126) at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:112) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:211) at org.hibernate.engine.jdbc.batch.internal.NonBatchingBatch.addToBatch(NonBatchingBatch.java:62) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3124) at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:3581) at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:104) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463) at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349) at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350) at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56) at org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222) at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425) at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101) at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177) at hibernate_tut_emp.MyOps.main(MyOps.java:38) Caused by: java.sql.SQLException: Lock wait timeout exceeded; try restarting transaction at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2582) at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2530) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1907) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2141) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2077) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2062) at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.executeUpdate(ResultSetReturnImpl.java:208) ... 13 more 
+1
source share
1 answer

As pointed out by @JBNizet in the comments ...

I also had a table open on the ubuntu terminal when you tried to perform operations on it using hibernate. This prevented the operation from being performed on the table. I closed it and now worked great.

To access the table, I use the database.tableName method from another schema to access it now, so avoid the deadlock!

0
source

Source: https://habr.com/ru/post/979909/


All Articles