Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.sql.SQLIntegrityConstraintViolationException:ORA-00001:运行Oracle函数时的唯一约束_Java_Plsql_Oracle11g_Mybatis_Spring Transactions - Fatal编程技术网

java.sql.SQLIntegrityConstraintViolationException:ORA-00001:运行Oracle函数时的唯一约束

java.sql.SQLIntegrityConstraintViolationException:ORA-00001:运行Oracle函数时的唯一约束,java,plsql,oracle11g,mybatis,spring-transactions,Java,Plsql,Oracle11g,Mybatis,Spring Transactions,我得到java.sql.SQLIntegrityConstraintViolationException:ORA-00001:使用mybatis运行oracle函数时出现唯一约束错误。我将spring事务配置为在Serializable中运行,readOnly为false。下面是我的映射器类 public interface ILockMapper { @Transactional(isolation=Isolation.SERIALIZABLE, readOnly=false)

我得到java.sql.SQLIntegrityConstraintViolationException:ORA-00001:使用mybatis运行oracle函数时出现唯一约束错误。我将spring事务配置为在Serializable中运行,readOnly为false。下面是我的映射器类

public interface ILockMapper {

    @Transactional(isolation=Isolation.SERIALIZABLE, readOnly=false)
    String aquireLock(final SpInOutFields input);

    @Transactional(isolation=Isolation.SERIALIZABLE, readOnly=false)
    String releaseLock(final SpInOutFields input);

}
我的
aquireLock
方法运行我的oracle函数,oracle函数有两条insert语句。在插入数据之前,我对给定数据使用select count(*)检查数据是否存在。如果存在,我不会插入下面的示例pl sql语句中的数据

SELECT COUNT(*) INTO rowcount FROM kp_lock WHERE device_id = deviceId; 


   if rowcount = 0 then
      INSERT INTO kp_lock(device_id,lock_flag,request_time) values ( deviceId, 'YES', CURRENT_TIMESTAMP);
  status := threadSysId;
   else
  status := '';
   end if;
当我从oracle运行该函数时,它工作正常。当我运行单线程时,它工作正常,但当我运行多线程时,它失败

我的JUNIT测试类如下所示

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations={"classpath:config/spring-context.xml"})
    public class TestSerialization {

        @Autowired
        ApplicationContext context;

        @Test
        public void testSerialize() throws InterruptedException{

            MultiThread multithread = context.getBean(MultiThread.class);
            MultiThread multithread1 = context.getBean(MultiThread.class);

            Thread thread = new Thread(multithread);
            Thread thread1 = new Thread(multithread1);

            thread.start();

            if(multithread == multithread1){
                System.out.println("Both refer same instance");
            }

            thread1.start();

try {
                thread.join();
                thread1.join();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

请帮助我配置事务以同步方式运行。不确定哪里出错了

问题在于函数的非原子性导致了竞争条件。在多线程环境中,在检查记录是否存在和插入之间可能会执行其他线程

仅通过事务配置无法解决此问题。你需要做的就是去做


看起来您正在尝试实现自定义锁。考虑使用

谢谢输入@罗马KooVal.我的问题有点复杂,我正在尝试实现FIFO和组(JMS方式,我不想添加JMS组件,这会使我的系统变得复杂)。让我检查一下用户定义的锁