简体   繁体   English

在JDBC中一次运行4个mysql查询

[英]Run 4 mysql queries at once in JDBC

I wrote a Java application to add articles to a Joomla site. 我编写了一个Java应用程序,将文章添加到Joomla网站。
I'm using JDBC to connect and run queries on Mysql database. 我正在使用JDBC连接并在Mysql数据库上运行查询。
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). 要创建文章,我需要在_contents表(一个插入)上运行一个查询,在_assets表(两个更新和一个插入)上_assets 3个查询。
the three queries that should be executed on _assets table are used to insert an item to a "Nested List Tree"; 应当在_assets表上执行的三个查询用于将一个项目插入“嵌套列表树”中; 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). 我的程序正在从远程系统访问数据库(因此Internet连接问题或其他任何问题都可能中断查询)。
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 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. 您也可以在结合了4个查询的“存储过程”中使用Mysql-Transactions This way I think it is more likely that your update succeeds (on the database, your Java-method may fail I think). 这样,我认为您的更新更有可能成功(在数据库上,我认为您的Java方法可能会失败)。 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 )). 但是,如果您仅使用一个Java程序,那么我将在Java端使用事务,因为对我而言,Java比sql更容易(尤其是如果您使用spring和诸如hibernate的持久性提供程序( Spring事务hibernate事务 ) )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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