函数调用问题
我将调用一个函数,该函数将从数据库中检索一些数据值。但在此之前我将发送这些数据。我只是通过此函数调用检查这些数据是否正确插入。但插入数据需要一些时间才能插入数据库,但我的函数调用在实际将数据插入数据库之前就开始了。因此,它发现数据库中没有插入任何数据。任何人都可以告诉我如何解决这个问题。怎么同步这个。正确插入数据库后我是否应该得到正确的结果。我不能在这里使用可运行的接口或线程类。我认为我要做的就是在一定时间后调用数据访问函数,以便数据有足够的时间插入数据库。请帮帮我。
i am going to call a function which will retrieve some data value from database. but before that i am sending those data. i am just checking whether those data properly inserted or not with this function call. but inserting data taking some time to insert into the database but my function calling starts before it actually inserts the data into the database. As because of that it is finding that no data is inserted in the database. Can any one tell how do i resolve this issue. How to synchronize this. whether i should get the proper result after the proper insertion into the database. i cant use here runnable interface or thread class. the think that i have to do is to call the data access function after certain time so that data gets enough time to get inserted into the database. please help me out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不知道您使用的是什么语言,但也许该函数有一个参数导致它等待查询完成后再返回?有提到“同步”这个词吗?
Don't know what language you are using, but maybe the function has a parameter which causes it to wait until the query finishes before returning? Something mentioning the word "synchronous?"
使用数据库驱动程序
我对 JDBC 不熟悉,所以我不确定哪些工具可供您使用,但看起来您所做的工作超出了您的需要。
通常,数据库驱动程序会通知您查询是否已成功执行,因此您不需要让应用程序随后查询数据来验证数据是否存在。相反,询问驱动程序是否有错误以查看是否存在查询有问题。
如果您要插入大量数据并且您的数据库支持它,您可能需要 使用事务来执行插入。这会将所有数据传递到数据库中,尝试插入,并警告您任何问题。
如果事务出现问题,您可以回滚,数据库状态将与开始时相同(显然您需要处理错误以保存数据)。如果没有问题,您就可以完成事务的提交,并且放心数据库状态与应用程序状态匹配。
替代方案
如果由于某种原因上述方法不起作用,您可以尝试使用事件模式来解决竞争条件。简而言之,您希望在数据插入完成时引发事件以提醒验证器可以开始读取数据。验证器将侦听该事件并在听到该事件时触发。
Use the Database Driver
I'm not familiar with JDBC so I'm not sure which tools are/aren't available to you, but it seems like you're doing more work than you need to.
Typically the database driver will inform you whether the query was executed successfully, so you should not need to have your application query the data afterward to verify that the data is there. Instead, ask the driver for errors to see whether there was a problem with the query.
If you're inserting a large about of data and your database supports it, you may want to use a transaction to perform your insert. This will pass all of the data into the db, attempt the insert, and warn you of any problems.
If there are problems with the transaction, you can roll back, and the database state will be the same as when you started (obviously you will need to handle the errors to save your data). If there are no problems, you can finish committing the transaction, and rest assured that the database state matches the application state.
Alternatives
If for some reason the above methods won't work, you can try to resolve the race condition using an event pattern. In simple terms, you want to raise an event when the data is done inserting to alert the validator that it can start reading data. The validator will listen for that event and trigger when it hears it.