preparedStatement.executeUpdate()不插入值

时间:2015-11-03 09:27:30

标签: java sql oracle jdbc

我在JBehave非Maven框架中工作,我正在运行一个插入语句,该语句位于一个文本文件中,我将其作为qry传递给我的方法。 insert语句是:

Insert into croutreach.ACME_OUTREACH_EMT_STG 
(SEQ,EID,EVENT_TYPE_HINT,LOADER_TYPE,CLIENT_ID_LIST,ACCT_NUM_LIST,PROCS_DT)
values (49,<EID>,'new','CI',null,null,'02-SEP-13 06.55.41.952000000 PM')

以下代码正在执行并打印以下声明

  

执行以运行插入查询

但在此之后它没有进入下一步,这就是为什么连接没有关闭。它被卡住preparedstatement.executeupdate()

private Connection dbConnection = null;
public boolean runInsertQuery(String qry) {
int result;
System.out.println("Preparing to run insert query");
try {
    PreparedStatement preparedStatement =dbConnection.prepareStatement(qry);
    System.out.println("Execuing to run insert query");
    result = preparedStatement.executeUpdate();
    System.out.println("Completed to run insert query");
    return result == 1;
    } catch (SQLException e) {
        LOG.fatal("Error in insertion into table  :", e);
        return false;
    } finally {
        this.disconnectDb();
    }
}

我的输出如下: -

Method public org.jbehave.core.configuration.Configuration com.TEA.framework.Functional.OnlineCoachingStory.configuration() has a @Test annotation but also a return value: ignoring it. Use <suite allow-return-values="true"> to fix this
Method public org.jbehave.core.steps.InjectableStepsFactory com.TEA.framework.Functional.OnlineCoachingStory.stepsFactory() has a @Test annotation but also a return value: ignoring it. Use <suite allow-return-values="true"> to fix this  
[TestNG] Running:  
  C:\Users\M41974\AppData\Local\Temp\testng-eclipse-1062921629\testng-customsuite.xml

Processing system properties {}  
Using controls EmbedderControls[batch=false,skip=false,generateViewAfterStories=true,ignoreFailureInStories=false,ignoreFailureInView=false,verboseFailures=false,verboseFiltering=false,storyTimeoutInSecs=900000000,threads=1]

(BeforeStories)

Running story com/TEA/framework/Story/Copy of OnlineCoaching.story  

(com/TEA/framework/Story/Copy of OnlineCoaching.story)  
Scenario: Setup the Environment  
2015-11-03 04:46:49,425 FATAL pool-1-thread-1- ACME.SAT.Server.Path not found in the Config.properties file  
2015-11-03 04:46:49,440 INFO pool-1-thread-1- Host:- lsh1042a.sys.cigna.com  Username:- ank_1   password:- abc_123 path:- null  
Given Set the environment to SAT  
2015-11-03 04:46:49,768 INFO pool-1-thread-1- DB Connection successful  
Insert into croutreach.ACME_OUTREACH_EMT_STG (SEQ,EID,EVENT_TYPE_HINT,LOADER_TYPE,CLIENT_ID_LIST,ACCT_NUM_LIST,PROCS_DT) values (49,'1234567890','new','CI',null,null,'02-SEP-13 06.55.41.952000000 PM')  
Preparing to run insert query  
Executing to run insert query  
|

之前我保持我的SQL开发人员窗口打开,在关闭后再次尝试并再次运行相同的脚本时,它显示致命错误asORA-00001:违反了唯一约束。

我面临三个不同的问题:

  1. 当SQL Developer处于打开状态时,运行脚本会卡在插入位置。

  2. 虽然卡住了但我关闭了SQL Developer窗口会话,之后输出如下:

    2015-11-03 05:01:35,061 INFO main- DB Connection successful
    Insert into croutreach.ACME_OUTREACH_EMT_STG (SEQ,EID,EVENT_TYPE_HINT,LOADER_TYPE,CLIENT_ID_LIST,ACCT_NUM_LIST,PROCS_DT) values (49,'02123456789','new','CI',null,null,'02-SEP-13 06.55.41.952000000 PM')
    Preparing to run insert query
    Executing to run insert query
    Completed to run insert query
    2015-11-03 05:02:11,128 INFO main- DB Disconnected!!
    Insert complete
    
  3. 关闭SQL Developer窗口后,再次运行脚本但出现致命错误,如下所示:

    2015-11-03 05:13:08,177 INFO main- DB Connection successful
    Insert into croutreach.ACME_OUTREACH_EMT_STG (SEQ,EID,EVENT_TYPE_HINT,LOADER_TYPE,CLIENT_ID_LIST,ACCT_NUM_LIST,PROCS_DT) values (49,'02123456789','new','CI',null,null,'02-SEP-13 06.55.41.952000000 PM')
    Preparing to run insert query
    Executing to run insert query
    2015-11-03 05:13:08,319 FATAL main- Error in insertion into table  :
    java.sql.SQLException: ORA-00001: unique constraint (CROUTREACH.ACME_OUTREACH_EMT_STG_PK) violated
    
        at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
        at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
        at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:743)
        at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:216)
        at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:955)
        at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1168)
        at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3316)
        at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:3400)
        at com.TEA.framework.Technology.DatabaseClass.runInsertQuery(DatabaseClass.java:169)
        at com.TEA.framework.Business.AcmeDB.insertACME_OUTREACH_EMT_STG(AcmeDB.java:186)
        at com.TEA.framework.Technology.MainClass.DBinsertTest(MainClass.java:123)
        at com.TEA.framework.Technology.MainClass.main(MainClass.java:24)
    2015-11-03 05:13:08,350 INFO main- DB Disconnected!!
    2015-11-03 05:13:08,365 INFO main- DB Disconnected!!
    

1 个答案:

答案 0 :(得分:2)

您只是看到一个会话阻止了另一个会话。当您在SQL Developer中执行插入并使会话保持运行而不提交或回滚时,您将锁定新插入的行和(某种待处理的)索引条目。

如果您运行程序并再次执行相同的插入,则会在另一个会话中执行。它无法看到您通过SQL Developer插入的数据,但它可以看到锁定,并等待清除该锁定。虽然它被阻止了它只是坐在那里等待,你正在解释它被卡住了。

如果您回滚SQL Developer插件,则会清除锁定,然后您的程序被阻止插入,然后成功继续。插入新记录,并根据您的代码下一步做什么或连接的自动提交设置提交或回滚。

它看起来像是提交,因为再次运行会尝试重复插入,现在它确实会看到现有的已提交数据 - 因此会出现ORA-00001错误,因为您正在尝试创建第二个条目相同的主键值。如果你提交了SQL Developer会话而不是回滚,你会看到同样的事情。

如果您想再次运行程序,则需要在再次尝试插入之前删除该行(并提交更改!)。