如何同步PL/SQL调用的Java方法 [英] How to synchronize a java method called by PL/SQL

查看:75
本文介绍了如何同步PL/SQL调用的Java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

与并发有关的一个问题是,当客户端(称为Oracle Forms)提交请求(称为并发程序)并调用plsql过程时,该逻辑流程最终将调用java静态方法.

I just have a problem relative to concurrency whose logic flow is when a client (called Oracle Forms) will submit a request (called concurrent program ) and call a plsql procedure, this procedure eventually will call a java static method.

我发现,当我同时或在很短的间隔(例如1秒)内提交两个请求时,会发现一些并发问题.

What I find is that when I submit two request in the same time or in a very short interval(like 1 second), some concurrency problem will be noticed.

java方法是从数据库中搜索建议将哪些记录插入数据库的操作的起点.

The java method is the start point of doing something that search from the database suggest that which records should be inserted into database.

问题在于,它们将导致重复的结果,因为当我查询时,两个请求都发现插入新记录很好.

The problem is that, they will result in duplicated result since when I query, both request find it fine to insert new records.

我尝试在静态java方法中添加synchronized,但这不能解决此问题,为什么?

I tried add synchronized in the static java method, but this does not solve this problem, why?

我要做的是:

public static synchronized void execute

请注意,insert将在plsql中调用,这意味着如果仅同步java方法,我将无法进行足够的同步.但是当我查看日志时,它显示两个请求在同一秒内运行,我认为这是不正常的!因为查询数据库和执行建议非常耗时.

Please note that the insert will be called in plsql, which means I do a not sufficient synchronize if only synchronize the java method. But when I look into the log, it shows the two request run in the same second, which I do not think it is normal! since query database and doing the suggestion is time consuming.

要使java方法真的很费时,我添加了一个代码调用Thread.sleep(5000),并在此代码之后记录时间并记录线程ID.

To make the java method really time consuming, I add a code call Thread.sleep(5000), and log for the time after this code and log the thread id.

惊奇地发现Thread id是1!而且,他们通过睡眠的时间是在同一时间.为什么呢?

Surprise to see that the Thread id is 1! And also, the time where they pass the sleep is in the same time. Why is that?

我该怎么做才能解决问题? java方法或pl sql是否有锁?

What can I do to solve the problem? Any lock on the java method or pl sql?

PS:我现在正在尝试使用DMBS_LOCK,它似乎正在工作,但是我仍然希望知道java方法未同步的原因.

PS: I am now trying to use DMBS_LOCK, and which seems to be working but I still hope to know the reason why the java method is not synchronized.

推荐答案

我不知道如何在Oracle DB中实现JVM,但是由于(至少在某些常见配置中)每个数据库连接都拥有自己的服务器处理,然后,如果将单独的JVM嵌入其中的每个JVM中,那么同步块将无济于事.您需要使用数据库锁.

I have no idea how the JVM inside the Oracle DB is implemented, but since (at least in some common configurations) every database connection gets its own server process, then if a separate JVM is embedded into each of those, a synchronized block won't do you much good. You'd need to use database locks.

这篇关于如何同步PL/SQL调用的Java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆