简体   繁体   中英

Run 4 mysql queries at once in JDBC

I wrote a Java application to add articles to a Joomla site.
I'm using JDBC to connect and run queries on Mysql database.
To create an article I need to run a query on _contents table (an insert) and 3 queries on _assets table (Two Updates & One Insert).
the three queries that should be executed on _assets table are used to insert an item to a "Nested List Tree"; so if one of these queries fail the Nested List Tree will break.
My program is accessing to database from a remote system (So Internet connectivity problems or anything else can interrupt queries).
How can I get sure that all three queries run with each other without any of them get missed?

Thanks

I think transactions is what you are looking for.

http://docs.oracle.com/javase/tutorial/jdbc/basics/transactions.html

You may also use Mysql-Transactions in a "stored procedure" that combines your 4 queries. This way I think it is more likely that your update succeeds (on the database, your Java-method may fail I think). and if you are planing to use other programs/ programing languages the handling of your update will be the same and you won't have to care any further.

However, if you are just using one Java program, I would use transactions on the Java-side, because for me Java is easier than sql (especially if you use spring and a persistence provider like for example hibernate ( Spring Transactions , hibernate transactions )).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM