Handle the row lock in multithreaded environment Using jdbcTemplate - spring

I'm working on Spring JPA and JdbcTemlapte mixed environment. Challenge is to update a row(Oracle) using jdbcTemplate in a multithreaded environment. So the question is how to lock the row and how to prevent deadlock scenarios?

The solution is to use FOR UPDATE NOWAIT; end of the select query. This avoids waiting for other transactions to release row locks.
Found a solution on oracle documentation.

Related

Transaction management in database when two spring boot application trying to access the same record

Two spring boot applications are connected to the common database.
I just wanted to know, how to handle the transaction if both the application try to update the record at the same time?
Since you seem to use JPA (via Spring Data JPA) there isn't much to handle.
The database itself will prevent two transactions to update the record at the same time. So one will always be first.
If you use optimistic locking (which is the default with JPA) the second transaction will notice the modified row and rollback.
Without that the second transaction will simply overwrite the changes with it's own changes.

is getHibernateTemplate().flush() release data base connection

Please let me to know is getHibernateTemplate().flush() release data base connection after commit. If not what is the procedure to release data base connection.
I cant use hibernate transaction manager to resolve this.
calling flush be it on the HibernateTemplate or Session doesn't release anything it will only flush the pending sql statements to the database.
A connection will be releases as soon as the Session is closed, when this is depends on your setup.
Note: HibernateTemplate should be considered deprecated as of Hibernate 3.0.1 (this is when contextual sessions where introduced) and you should favor plain Session usage of the HibernateTemplate.
getHibernateTemplate().flush() will not release DB connection after commit. flush() will just synchronize your persistence context state with DB by firing update queries.
Connection will be closed when the hibernate session ends. However you can start a new transaction once the current one finishes.
Additional reference: function of getHibernateTemplate().flush()

Clojure JDBC transaction not rolling back on BatchUpdateException in HSQL

I'm writing a Clojure program using clojure.java.jdbc. I'm using DBCP to pool connections to HSQL 2.2.8. I have a (transaction) block in which I test if a schema exists, and if not, creates it and a bunch of tables. One of the statements after the schema create (I believe a MERGE statement) throws a BatchUpdateException.
The issue is that the schema create is not rolled back on the BatchUpdateException, even though they're part of the same (transaction) block.
Are there known issues with Clojure JDBC interacting with DBCP or HSQL?
Never mind.
Transactions don't apply to schema changes, apparently. WTF?

JBoss autocommit to Oracle doesnt work always

I have a very interesting situation. I am slightly new to JBoss and Oracle, having worked mostly with Weblogic on DB2. That said, what I am trying to do is pretty simple.
I have a local-tx-datasource to an Oracle database. From my Java I code, I invoke datasource.getConnection() after retrieving the datasource using the appropriate JNDI name. The local-tx-datasource declaration in my -ds.xml file does not have any explicit reference to autocommit behaviour.
After getting the connection, I execute a create/update query and I get back the correct update count. Subsequently, for a short duration, I am even able to retrieve this record. However, after that the database pretends it never got the record in the first place, and there is nothing at all.
My experience with connections suggests that this happens when the connection does not commit its work, and so only that connection itself will be able to see the data in its transaction. From what I read, JBoss too follows the specification that the Connection returned is an autocommit one. I even verified this from my Java code, and it states the autocommit behaviour is set to true. However, if that was the case, why are my records not getting created / updated?
Following this, I set the Connection's autocommit behaviour to false (again from Java code), and then did the commit explicitly. Since then, there has been no issue.
What could possibly be going wrong? Is my understanding of autocommit here incorrect or does JBoss have some other interpretation of it. Please note, I do not have any transactions at all. These are very simple single record insert queries.
Please note, I do not have any transactions at all.
Wrong assumption. The local-tx-datasource starts a JTA transaction in your behalf. I'm not sure how the autocommit works in this scenario, but I suppose that autocommit applies only when you are using exclusively JDBC transactions, not JTA transactions.
In JTA, if you don't commit a transaction[*], it will be rolled back after the timeout. This explains the scenario that you are experiencing. So, I'd try to either change the local-tx-datasource to no-tx-datasource or to manually commit the transaction.
Note, however, that not managing your transactions is a bad thing. Autocommit should always be avoided. There's no better party to determine when to commit than your application. Leaving this responsibility to the driver/container is, IMO, not very responsible :-)
[*] One exception is for operations inside EJBs, whose business methods are "automatically" wrapped in a JTA transaction. So, you don't need to explicitly commit the transaction.

jdbc - transaction manager needed by our web server for db transactions?

I'm using Jetty with mysql. I need some basic transaction support, and jetty is warning me at startup that no transaction manager is in use. I thought transactions were native to mysql? I'm trying something like:
Connection conn = ...;
conn.setAutoCommit(false);
// insert into table foo some data
// insert into table grok some data
conn.commit();
If an exception is thrown between the two statements, I see that data has made its way into table "foo", so the transaction calls did not work.
So I guess we really do need a transaction manager here, am I understanding this correctly? If so, I was looking at bitronix: http://docs.codehaus.org/display/BTM/Home
Thanks
Transactions are not "native" to MySQL (unlike other databases).
You need to make sure you are using the InnoDB storage engine, otherwise you won't be able to make use of transactions.

Resources