Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Other Forums
 Other Topics
 adAsyncExecute ?

Author  Topic 

aiken
Aged Yak Warrior

525 Posts

Posted - 2003-06-10 : 13:26:08
I'd like to fire off an asynchronous query from an ASP page. So far so good; I know how to do it.

However, closing and deleting the ADODB.Connection kills the asynchronously executing query.

But it's a best practice to always close DB connections from ASP; I've definitely seem memory leaks when that's not done.

So how can I accomplish what I need to without either making it synchronous (and therefore slow for the user), or generating memory leaks?

Any ideas appreciated.

Thanks
-b

nr
SQLTeam MVY

12543 Posts

Posted - 2003-06-10 : 13:51:07
The asychronous query is executing on the connection. Dropping the connection wil of course kill the query.
You need to leave the connection and drop it when the query completes as you would with a synchronous query.

This doesn't free up the connection just allows the application to do other things while waiting for the query to complete.
You also won't be able to do anything else on this connection for the duration.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

aiken
Aged Yak Warrior

525 Posts

Posted - 2003-06-20 : 14:58:47
Hmm... so what do do with an ASP page, where I want the page to finish and the user to be on to other things while the query executes? Do I just not close the DB on that page, and hope that ASP deals with the cleanup (or deal with a small memory leak)?

Thanks
-b

Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2003-06-20 : 15:27:32
ASP 3.0 or ASP.net?

Michael

<Yoda>Use the Search page you must. Find the answer you will.</Yoda>
Go to Top of Page

mohdowais
Sheikh of Yak Knowledge

1456 Posts

Posted - 2003-06-21 : 02:29:26
I dont think running an asynchronous query off an ASP page is such a good idea, for the very reasons you mention. The connection must be closed when the page is ready, but doing that will kill the query. I suggest you create a job and create a one-time job schedule to run the query/sp. Is the query relatively static, or is it parameterized? If it is static, you just need to create the job once and create a schedule to run it, say, 5 seconds later each time the ASP page is executed. Look up the following in BOL:

sp_add_job
sp_add_jobstep
sp_add_jobschedule

Owais

Go to Top of Page

Stoad
Freaky Yak Linguist

1983 Posts

Posted - 2003-06-21 : 04:58:23
I agree with Owais and maybe this help:

ss = "waitfor delay '000:02:00' insert into t select 5678"
cn.Execute "exec spAsyncSQL " & Chr(34) & ss & Chr(34)
cn.Close
Set cn = Nothing

Here this user-defined stored procedure spAsyncSQL (tested on 7.0 and
works under running SQL Server Agent):

-- spAsyncSQL Stored Procedure for asynchronous start of T-SQL batch

CREATE PROCEDURE spAsyncSQL @tsql varchar(3200) AS

set nocount on

declare
@jname varchar(70),
@jobid uniqueidentifier,
@nt_user_name NVARCHAR(100),
@dt datetime,
@itime int,
@start_date int,
@skey nchar(1)

set @jobid = NEWID()
set @jname=LTRIM(RTRIM('ZZZ Async T-SQL Batch Executer '+convert(varchar(36),@jobid)))
set @nt_user_name=IsNull(NT_CLIENT(),SUSER_SNAME())
set @skey=N'S'

INSERT INTO msdb.dbo.sysjobs
(job_id, originating_server, name, enabled, description, start_step_id, category_id,
owner_sid, notify_level_eventlog, notify_level_email, notify_level_netsend, notify_level_page,
notify_email_operator_id, notify_netsend_operator_id, notify_page_operator_id,
delete_level, date_created, date_modified, version_number)
VALUES
(@jobid, N'(LOCAL)', @jname, 1, NULL, 1, 0,
SUSER_SID(), 2, 0, 0, 0, 0, 0, 0,
3, GETDATE(), GETDATE(), 1)

INSERT INTO msdb.dbo.sysjobsteps
(job_id, step_id, step_name, subsystem, command, flags, additional_parameters,
cmdexec_success_code, on_success_action, on_success_step_id, on_fail_action,
on_fail_step_id, server, database_name, database_user_name, retry_attempts,
retry_interval, os_run_priority, output_file_name, last_run_outcome, last_run_duration,
last_run_retries, last_run_date, last_run_time)
VALUES
(@jobid, 1, 'TheOnlyStep', N'TSQL', @tsql, 0, NULL, 0,
1, 0, 2, 0, NULL, DB_NAME(), NULL, 0,
0, -15/*-1*/, NULL, 0, 0, 0, 0, 0)

INSERT INTO msdb.dbo.sysjobservers
(job_id, server_id, last_run_outcome, last_outcome_message,
last_run_date, last_run_time, last_run_duration)
VALUES
(@jobid, 0, 5, NULL, 0, 0, 0)

IF @@trancount>0
BEGIN
set @skey=N'U'
set @start_date=datepart(yy,getdate())*10000+datepart(mm,getdate())*100+datepart(dd,getdate())
set @dt=DateAdd(ss,10,GETDATE()) -- Start after 10 sec. It is needed for the job's cashing.
-- Increase this number if your transactions are very long.
set @itime = datepart(hh,@dt)*10000+datepart(mi,@dt)*100+datepart(ss,@dt)
INSERT INTO msdb.dbo.sysjobschedules
(job_id, name, enabled, freq_type, freq_interval, freq_subday_type, freq_subday_interval,
freq_relative_interval, freq_recurrence_factor, active_start_date, active_end_date,
active_start_time, active_end_time, next_run_date, next_run_time)
VALUES
(@jobid, 'TheOnlySchedule', 1, 1, 0, 0, 0, 0, 0, @start_date, 99991231,
@itime, 235959, 0, 0)
END

EXEC master.dbo.xp_sqlagent_notify N'J', @jobid, NULL, NULL, @skey, @nt_user_name, 0, @@trancount



Go to Top of Page
   

- Advertisement -