MSSQL数据库中的间歇性INSERT故障

时间:2019-02-03 16:11:56

标签: sql-server jdbc

我有一个处理相对大量交易的系统。在繁忙的一天,我有一个或两个事务未能插入到特定表中。没有异常被抛出。实际上,会创建自动增量ID,但不会插入事务。所有其他插入件都可以正常工作。我怀疑与锁有关。

我正在sql-server 2016中使用以下存储过程。在应用程序端,该语言是Java,JBODS EAP 7中具有托管数据源的JTDS驱动程序。

/*    ==Scripting Parameters==

    Source Server Version : SQL Server 2016 (13.0.4206)
    Source Database Engine Edition : Microsoft SQL Server Enterprise Edition
    Source Database Engine Type : Standalone SQL Server

    Target Server Version : SQL Server 2017
    Target Database Engine Edition : Microsoft SQL Server Standard Edition
    Target Database Engine Type : Standalone SQL Server
*/
USE [DB_NAME]
GO
/****** Object:  StoredProcedure [dbo].[sp_NAME_OF_SP]    Script Date: 2/3/2019 6:48:30 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER proc  [dbo].[sp_NAME_OF_SP]            
   --a list of parameters
AS   
Declare
--some declarations

--check for duplicate transaction ahving fiedl37 same in db
IF EXISTS(SELECT TOP 1 FIELD37 FROM TB_NAME WHERE FIELD37=@FIELD37 )
BEGIN
       set @returnvalue= -1
 return @returnvalue   
END
    declare

     @ErrorMessage VARCHAR(999) ,
          @ErrorSeverity VARCHAR(999) ,
    @ErrorState VARCHAR(999) 

BEGIN TRY
--  begin transaction
    if len(@FIELD2) > 15 and @FIELD2 <> ''
            begin
            set @FIELD2_BIN = SUBSTRING(@FIELD2, 1,6)
            set @FIELD2_LAST4 = SUBSTRING(@FIELD2, -5, len(@FIELD2))
            set @FIELD2  = @FIELD2_BIN+'XXXXXX'+@FIELD2_LAST4
    end 
           if   len( @FIELD4)= 0      
begin
set @FIELD4='0'
end
           if   len( @FIELD26)= 0      
begin
set @FIELD26='0'
end
if   len( @FIELD27)= 0      
begin
set @FIELD27='0'
end
if   len( @FIELD28)= 0      
begin
set @FIELD28='0'
end
if   len( @FIELD29)= 0   or @FIELD29 = 'CC'   
begin
set @FIELD29='0'
end

           if   len( @FIELD30)= 0      
begin
set @FIELD30='0'
end

if  len( @FIELD24)= 0  
begin
set @FIELD24 = '0'
end

if  len( @FIELD25)= 0  
begin
set @FIELD25 = '0'
end
--select set @FIELD24 = isnull(@FIELD24,'0')


if @field3 in ('360000', '310000','510000', '380000', '340000','370000','180000')
begin
set @field4 = 0
end

if @field0 in ('1420', '0420')
begin
set @field4 = (@field4*-1)
end
begin tran

INSERT INTO TB_NAME(   

    --a list of columns
 )             
values( 
--a list of values
) 
commit tran

if  len(@FIELD101) > 0  
begin
UPDATE ANOTHER_TB_NAME SET some_field = CAST(getDate() AS date) WHERE Column_NAME=@FIELD101 
end
 --set @returnvalue= @@IDENTITY
-- commit transaction
set @returnvalue = SCOPE_IDENTITY() 
 --return @returnvalue    

END TRY
 BEGIN CATCH
  rollback tran
    SET @ErrorMessage  = ERROR_MESSAGE()
    SET @ErrorSeverity = ERROR_SEVERITY()
    SET @ErrorState    = ERROR_STATE()

begin tran
insert into TB_CAPTURED_ERRORS (Error_Code
      ,Error_Narration
      ,Error_Source
      ,Narration
      ) values ('6',@ErrorMessage, 'SP_NAME-refNo-:'+@FIELD37,'Failed')
      commit tran
    set @returnvalue=-1
 return @returnvalue 

END CATCH

0 个答案:

没有答案