Error while mysql table gets updated from java - java

I am trying to update one particular record in mysql table from java code. Update function doing lots of different activities including the update operation within the same transaction.
But here the same table row is not getting updated from outside (MySQL Workbench) or other transaction until the transaction for update gets committed. Its generating the following error.
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLTransactionRollbackException: Lock wait timeout exceeded; try restarting transaction
Is there any work around to fix the issue? Please suggest.

Related

Spring JPA + MySQL Deadlock found on commit but the deadlock is not registered into the error_log of the database

I have a method annotated with #Transactional which obtain messages and perform some operations on the database to persist things correctly.
Now, some times this method throws a DeadLock with the following stacktrace:
Caused by: org.hibernate.TransactionException: Unable to commit against JDBC Connection
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.commit(AbstractLogicalConnectionImplementor.java:87)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:272)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:104)
at org.springframework.orm.jpa.JpaTransactionManager.doCommit(JpaTransactionManager.java:532)
... 34 common frames omitted
Caused by: com.mysql.cj.jdbc.exceptions.MySQLTransactionRollbackException: Deadlock found when trying to get lock; try restarting transaction
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:123)
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:97)
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122)
at com.mysql.cj.jdbc.ConnectionImpl.commit(ConnectionImpl.java:813)
at com.zaxxer.hikari.pool.ProxyConnection.commit(ProxyConnection.java:361)
at com.zaxxer.hikari.pool.HikariProxyConnection.commit(HikariProxyConnection.java)
at org.hibernate.resource.jdbc.internal.AbstractLogicalConnectionImplementor.commit(AbstractLogicalConnectionImplementor.java:81)
... 37 common frames omitted
The problem I am facing is that on the database side, MySQL is not registering any deadlock to happen in that moment.
I've enabled the innodb_print_all_deadlocks variable and I verified that it is working as expected by manually raising deadlocks which are immediately logged by the db.
I've also verified that when a real deadlock happens within the application (with real I mean that MySQL logs the deadlock on its side), the stacktrace is different and usually the deadlock is found by the database server way before the commit, for example when the application tries to ClientPreparedStatement.executeUpdateInternal.
It looks weird to me that the deadlock is raised on ConnectionImpl.commit. I've checked the ConnectionImpl code and I can't understand why a deadlock could be thrown there (at the line in the stacktrace, there's just return; )
Is it possible that JPA is somehow raising a deadlock on the app side?
Leaving this as it might help other people stuck with the same weird problem.
The reason for my problem was the fact that the application was using MariaDB driver instead of MySQL one. For some reasons, the MariaDB driver was throwing fake deadlock exceptions, so the MySQL server was correct in not logging any deadlock happening.
Switching the driver with the more correct one, all problems stopped.

Flyway 4.2.0 Multiple Nodes In Parallel Failure with Oracle 11g

I have multiple application servers configured to run flyway at startup. Each server attempts to apply the same set of migrations across multiple schemas in the same Oracle 11g database. These servers are started at the same time. This works most of the time. On occasion, however, a server fails during migration because it encounters a unique constraint violation.
Unable to insert row for version '0' in metadata table "FOO"."SCHEMA_VERSION"
SQL State : 23000
Error Code : 1
Message : ORA-00001: unique constraint (FOO.SCHEMA_VERSION_pk) violated
at org.flywaydb.core.internal.metadatatable.MetaDataTableImpl.addAppliedMigration(MetaDataTableImpl.java:242)
at org.flywaydb.core.internal.metadatatable.MetaDataTableImpl.addBaselineMarker(MetaDataTableImpl.java:334)
at org.flywaydb.core.internal.command.DbBaseline$2.call(DbBaseline.java:135)
at org.flywaydb.core.internal.command.DbBaseline$2.call(DbBaseline.java:112)
at org.flywaydb.core.internal.util.jdbc.TransactionTemplate.execute(TransactionTemplate.java:75)
at org.flywaydb.core.internal.command.DbBaseline.baseline(DbBaseline.java:112)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:990)
at org.flywaydb.core.Flyway$1.execute(Flyway.java:971)
at org.flywaydb.core.Flyway.execute(Flyway.java:1464)
at org.flywaydb.core.Flyway.migrate(Flyway.java:971)
...
I thought that flyway would be able to handle this situation based on the following:
https://flywaydb.org/documentation/faq#parallel
Shouldn't a flyway instance detect that the schema version table is locked and move onto the next schema?
Is there a setting that can ensure the schema version is locked or is this a bug?
The OracleTable class locks the table in exclusive mode. Should it add the NOWAIT clause and handle any resulting Oracle exception?
It should work. We test this with every build and the behavior without NOWAIT is the one we desired (block until lock is released). If you can reliably reproduce this or see a clear mistake in our code, then please by all means do file a bug with the necessary details in the issue tracker.

Java JDBC executeUpdate not working on Delete SQL with TRUNC function in where clause

The following delete statement is working fine in SQL developer but when executed using JDBC api executeUpdate() is not working.
After removing the where clause its working fine.
Delete from Tab1
where TRUNC(CREATED_TS) <= TRUNC(ADD_MONTHS(SYSDATE,-3))
I am not able to figure out the problem as no exception or error is printed.Only code execution is getting hanged at the executeUpdate() method.
Database : Oracle 11g
Java: 1.6
Since there is no Exception or Stack Trace one can only guess.
Probably the Exception is being swallowed in the Java code. Have a look at that and Print it if possible.
where TRUNC(CREATED_TS) <= TRUNC(ADD_MONTHS(SYSDATE,-3)) will prevent index on CREATED_TS (if there was one) from being used and may slow down the process. And a timeout may have occurred. I would check the connection/statement timeout settings on the Java side of the setup.
The issue was not with the delete SQL.The issue was with another session in which there was some uncommitted changes.Because of that the delete SQL from java code was getting hanged indefinitely. On issuing a commit on the another session ,java api executeUpdate() responded and it started working fine.
The question is similar to the issue as in the below link:
Oracle database is hanging infinitly in UPDATE queries

locked by transaction: #console:Oracle (INTELLIJ CLIENT)

I am working on IntelliJ IDEA 14.1.4, Recently we connected database to the Client and it was working okay, until some recent times when we start getting error
[2015-09-28 10:12:55] locked by transaction: #console:Oracle - <<DBName>>#localhost
Now we can't perform any transaction as we keep getting the same error. We tried googling the error but could not find anything to solve the problem. Any help would be appreciated!!!
I had this happen after a previous query I ran failed -- using PyCharm, not IntelliJ, but it is the same JetBrains system. I did not have Auto-commit turned on. Pycharm couldn't recover from the failed query for some reason, and it left an unresolved transaction active in the database. I disconnected from the database (hit the red stop button in the database window), and then I was able to resume with new queries with no problem. You might also try the rollback button at the top of the window with your SQL statements if reconnecting would cause you problems.
Putting up answer for the same comment.
I think there should be some row lock on the DB. if you have an Oracle DBA, check out for table/row lock. It can happen if the transaction exception occurred and rollback didn't happen properly or a transaction is still open for a longer time.
You can execute the following query to check for the same if transaction is pending.
SELECT COUNT(*)
FROM v$transaction t, v$session s, v$mystat m
WHERE t.ses_addr = s.saddr
AND s.sid = m.sid;
Additional resources :
Oracle: How to find out if there is a transaction pending?
How to find locked rows in Oracle
Consult with your DBA after your initial research. I'm not aware about releasing locks.

Session commit does not work for error:ORA-00054: resource busy and acquire with NOWAIT specified or timeout expired

My application that uses Spring JDBC, has a "Select For Update...wait 1" sql which issues a lock on the table row. I do a commit after that so that the lock gets removed. This is working in the jetty (Jetty data source is org.apache.commons.dbcp.BasicDataSource) but not in tomcat (org.apache.tomcat.dbcp.dbcp.BasicDataSource).
I commit as shown below: getJdbcTemplate().getDataSource().getConnection().commit()
Any help, appreciated !!

Categories

Resources