Executing a SSIS package from stored procedure is timing out - stored-procedures

I have a conundrum and I'm stumped and hoping y'all can help.
Here is my scenario: I am loading data into our database through a web interface. First, I upload the file to the SQL box, then I call the script which extracts and loads the data into SQL.
I recently changed this process so that now, instead of executing the package from within the web application, I am calling a stored procedure to execute the script.
Here is the core of my code:
CREATE PROCEDURE [dbo].[ExecuteSsisPackage]
#Package NVARCHAR(100),
AS
BEGIN
SET NOCOUNT ON;
DECLARE #executionID BIGINT
EXEC SSISDB.catalog.create_execution
#folder_name = 'Imports', -- nvarchar(128)
#project_name = 'DataImports, -- nvarchar(128)
#package_name = #Package, -- nvarchar(260)
#use32bitruntime = 1, -- bit
#execution_id = #executionID OUTPUT -- bigint
SELECT #executionId
EXEC [SSISDB].[catalog].[set_execution_parameter_value] #executionId, 50, N'SYNCHRONIZED', #parameter_value = 1 -- turn on synchronization
EXEC ssisdb.catalog.start_execution #executionID;
RETURN (#executionID)
END
Here is the odd part: when I run the stored procedure from management studio it executes fine. However, when I execute this from the web application it times out. But, if I turn OFF synchronization it executes just fine through the web app. The problem with disabling synchronization is that I need to pull some summary info off the data after it has been loaded.
I've even tried rolling my own 'synchronization' from a post I found:
DECLARE #status AS BIGINT = 1;
WHILE(#status = 1 OR #status = 2 OR #status = 5 OR #status= 8)
BEGIN
PRINT 'Status: ' + CAST(#status AS VARCHAR(10))
PRINT 'waiting 2 seconds for Package to finish'
WAITFOR DELAY '00:00:2';
SET #status = (SELECT [Status] FROM SSISDB.[catalog].[executions]
WHERE execution_id = #executionID);
END
PRINT 'Exit Status: ' + CAST(#status AS VARCHAR(10))
This also times out. I did some digging and found that when the SP is executing from the web (but not management studio) there is a lock: LCK_M_S and the wait_resource is: KEY: 9:72057594041532416 (d28bcaafb8df). Again, this only occurs when executing the SP from the web app.
Some more digging unearthed the culprit for the lock. It is the PK_Operations key of the SSISDB.internal.operations table. This is the conundrum. Any ideas why this occurs only through the web and when synchronized it turned on??

Related

Getting invalid SQL syntax upon executing stored procedure via DB2 CLI

I am working on a stored procedure to update values in a couple of tables in one go. I am getting CLI0118E Invalid SQL syntax. during the execution. However, I am not able to find the issue with my stored procedure as I am not quite familiar with the whole stored procedure rules. Any help on this would be much appreciated.
CREATE OR REPLACE PROCEDURE MYPROCNAME(IN VAL1 VARCHAR(255),IN VAL2 VARCHAR(255),IN VAL3 VARCHAR(255),IN VAL4 VARCHAR(255))
LANGUAGE SQL
BEGIN ATOMIC
UPDATE SCHEMA1.TABLE1 SET col1=VAL2, col2=VAL3, col3=VAL4 WHERE col1=VAL1;
UPDATE SCHEMA2.TABLE1 SET col1=VAL3, col2=VAL3, col3=VAL4 WHERE col1=VAL1;
FOR v AS
SELECT col1 FROM SCHEMA2.TABLE2 WHERE col2=VAL1
DO
UPDATE SCHEMA2.TABLE3 SET col2=VAL2 WHERE col1=v.col1 AND col3='<<value>>';
UPDATE SCHEMA2.TABLE3 SET col2=VAL3 WHERE col1=v.col1 AND col3='<<value2>>';
END FOR;
END
#
In the FOR statement, I even tried like this FOR v AS cur1 CURSOR FOR
Used this to compile the procedure first -- db2 -td# -vf myprocname.db2
Then, tried executing the procedure using -- db2 call MYPROCNAME 'val1','val2','val3','val4'
Also, tried the execution in diff combinations like db2 call MYPROCNAME ('val1','val2','val3',val4'), db2 call MYPROCNAME("val1","val2","val3","val4"), etc...
If I use () in the db2 call, I get the bash: syntax error near unexpected token (' error.
PS: I am using db2 11.5
As mentioned in the comment to my question above, running the following db2 "call MYPROCNAME ('val1', 'val2', 'val3', 'val4')" fixes the issue

Unable to cancel a query in DB2

I have the following stored procedure in DB2:
CREATE OR REPLACE PROCEDURE CANCEL_ACTIVITY (IN application_handle INTEGER)
LANGUAGE SQL
BEGIN
DECLARE UOW_ID INTEGER;
DECLARE ACTIVITY_ID INTEGER;
FOR v AS cur1 CURSOR FOR
SELECT UOW_ID, ACTIVITY_ID FROM TABLE(SYSPROC.MON_GET_ACTIVITY(application_handle, -1))
DO
CALL WLM_CANCEL_ACTIVITY(application_handle, v.uow_id, v.activity_id);
END FOR ;
END
Using the following query, I am able to find my connection ID:
SELECT MON_GET_APPLICATION_HANDLE() from SYSIBM.SYSDUMMY1
Which would return a value like 36547. So I call the procedure I just created like so:
CALL CANCEL_ACTIVITY(36547);
As a result, I get the following:
However, if I execute the query that gets connection IDs again, it doesn't seem like that connection ID is gone. I still get the 36547 value returned.
I am quite confused whether this query canceling is working at all. I am getting a range of different type of errors in different environments I am executing the code at.
When I am running it as a SQL query, I get the above error code / response. When my code is being executed in my webpage, I get the following error:
Cannot cancel queries: Java::ComIbmDb2JccAm::SqlDataException: DB2 SQL Error: SQLCODE=-802, SQLSTATE=22003, SQLERRMC=null, DRIVER=4.16.53
I am curious what I am doing wrong?
I would recommend to read Canceling Activies in the DB2 documentation. Canceling an activity is not closing a connection, but selectively aborting a query or other running tasks:
If an activity is consuming too many resources, or is running too
long, you can cancel it. Canceling an activity is gentler than forcing
the application that submitted the activity. A canceled activity
returns SQL4725N to the user, but does not end the connection or
affect any other user activity. Forcing the application ends both the
connection and user activities.
In your procedure you are looking for the app handle, uow and a specific activity ID. Have you looked what the activity you are going to cancel is/was doing? You could use WLM_CAPTURE_ACTIVITY_IN_PROGRESS to first dump information about that activity, so that you can debug your scenario.

db2 stored procedure error when update statement follows rollback

I have a stored procedure where at the end I check for errors and if there are errors I perform a rollback and then update the status on the batch table to 'FAILED'. When I run the stored procedure I regularly get an SQLCODE 818 error saying there is 'a timestamp conflict occurred'.
When I remove the update statement that changes the status on the batch table, I do not get the error.
What is the best practice to perform these actions so I avoid getting the error?
The section of code looks like this:
IF v_error_count > 0 THEN
-- Batch failed
ROLLBACK;
UPDATE batch_table bt
SET bt.batch_status = 'FAILED'
WHERE batch_id = input_batch_id;
END IF;
Thanks for any help.
SQL Code -818 indicates that the internal timestamp DB2 uses to ensure consistency between the running module matches the DBRM version created when the SQL Statements were precompiled.
You might check with your DBA (or someone else at your site), because the specific steps you have to perform may vary. For a general overview, you can see this article on IBM Knowledge Center.

SQL Server 2012 Stored Procedure timing out. How to fix it?

Context: SQL Server 2012, Windows Server 2008 R2 (64bit), 2 cores, lots of RAM and HD space,
ADODB via JScript (not .NET)
These really simple stored procedures keep timing out. It's not like I have a lot of records either (from a server point of view): 100,000 or so.
CREATE PROCEDURE [dbo].[Transfer_Part1]
AS
SET NOCOUNT ON
INSERT INTO Primary.dbo.Post
SELECT *
FROM Secondary.dbo.Pre
WHERE HrefProcessed = (-1)
AND ReferrerProcessed = (-1);
CREATE PROCEDURE [dbo].[Transfer_Part3]
AS
SET NOCOUNT ON
DELETE
FROM Secondary.dbo.Pre
WHERE HrefProcessed = (-1)
AND ReferrerProcessed = (-1);
Is there anything I can do to stop getting these messages?
[Microsoft][ODBC SQL Server Driver]Query timeout expired
EXEC Transfer_Part1
[Microsoft][ODBC SQL Server Driver]Query timeout expired
EXEC Transfer_Part3
Do you have an 2-column index on HrefProcessed and ReferrerProcessed?
Also see Bulk DELETE on SQL Server 2008 (Is there anything like Bulk Copy (bcp) for delete data?) for the second one.

What is the correct syntax to use when trying to create a Data Source View to a linked server?

I have tried several statements but this one at least returns data.. but I get the error message: Deferred prepare could not be prepared. Incorrect syntax near')'. Incorrect syntax near the keyword 'DECLARE'. The following statement executed when creating namedquery:
SELECT[vwStatistics].*
FROM
(
***THIS IS MY QUERY***
DECLARE #SQL1 VARCHAR(500)
SET #SQL1 = 'SELECT *
FROM OPENQUERY(PORTAL, ''SELECT DeviceID, Date, Count
FROM printer_stats.Statistics
GROUP BY DeviceID'')'
EXEC (#SQL1)
***END OF MY QUERY***
)
AS[vwStatistics] (Microsoft.AnalysisServices.Controls)
I am new to linked servers and to SSAS. This is our company's first Cube from a linked server. My query does run in Management Studio and creates a SSRS report but it is slow.
Any suggestions would be helpful. Not much info on syntax for this situation on web. I have been looking for any such situation and have not found much help other than trying changes on server. EX: Make sure openrowset is on and reinstall OWC component.. I do not have that capability.
This is what we found to work:
SELECT DeviceID, CAST(statsdt AS CHAR) AS sdt, Count FROM OPENQUERY (
PORTAL, 'select * from (select DeviceID,CAST( Date AS CHAR) statsdt, Count from printer_stats.Statistics) as pstats')

Resources