I am trying to trigger a url from a trigger in SQL Anywhere and not wait for the response.
In SQL Anywhere I have the following function:
CREATE FUNCTION "DBA"."sendCallback"( in @serverip text,in @url text )
returns char(255)
not deterministic
external name 'Callback.dll::Namspace.Callback.OpenUrlAsync(string, string) string' language CLR
And in C# I have the following program:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Net;
using System.ComponentModel;
namespace Namspace
{
public static class Callback
{
public static string OpenUrlAsync(string server, string url)
{
try
{
Thread t1 = new System.Threading.Thread
(() =>
{
using (WebClient wc = new WebClient())
{
try
{
string result = wc.DownloadString(server + url);
}
catch (Exception ee)
{
Console.Error.WriteLine(ee.Message);
}
}
});
t1.Start();
}
catch (Exception e)
{
Console.Error.WriteLine(e.Message);
}
return "done";
}
}
}
When I call this from ISQL it works perfectly, but when it is called from the trigger it loks like the tread gets aborted before it can make the call.
If I add t1.Join() after it is started I get the desired effect, but then it uses to much time to be called from a trigger.
Is it correct that the CLR gets “teared down” when the connection calling it is finished?
How can I make sure the thread finishes?
You could wrap the Call in a SQL Anywhere Event.
In your Table Trigger you can then use
trigger event <MyEvent>triggers are executed within its own connection. If you have a lot of transactions you can run out of connections or overload the server. Be carefull!