blogs.conchango.com

welcome to the conchango blogging site
Welcome to blogs.conchango.com Sign in | Join | Help
in Search

SSIS Junkie

Conchango are busy and need talented consultants in and around London. Interested? Email me or send me a message

SSIS Nugget: Verify a data source before using it

As some of you may know I've been writing about SSIS on this blog for a lon-n-n-n-n-g time now. Too long it seems sometimes. In all that time though I've only had one short client engagement using the product which means that my real-world experience is somewhat limited. Studying something is all well and good but there's nothing quite like "doing it for real".

Well to that end I started on my first full time SSIS engagement recently. I've spent most of my time so far putting together our ETL Strategy which involves coming up with our mechanism for logging, source system access standards, best practices etc... all that sort of stuff. Today though was my first day proper of full on development and already I've learnt loads about the product that I'm going to try and share a little of here. They're only small things but the whole is definately greater than the sum of the parts in this case.

 

So onwards...

I wanted a way of verifying that I was able to connect to a data source before I actually attempt to pull data from it. Why did I want to do this? Well there are a few reasons:

1) Verify at the start of the package that all data sources are available. This saves on the package failing further down the line after lots of ultimately unnecassary processing

2) Gain some insight into exactly why the data source is unavailable rather than the less than useful messages that SSIS can sometimes give you.

3) Throw custom errors pertianing to the exact cause of the failed connection.

4) Verify the connection string of the connection manager used at runtime. This may not be the same as that set at design-time if it is affected by a configuration or an expression.

5) Conditional workflow based on the cause of the failure

 

As usual I turned to the trusty Script Task to enable me to do this. Sure a custom task would have been better, and that's definately the next step, but for now I'll stick with the script task. It makes demoing this easier as well!

Here's an example that I've put together. It connects to a Connection Manager called "AdventureWorks" and passes out some useful data depending on the success thereof. The real trick is in the call to the AcquireConnection() method of the connection manager. Take a look.

20051010VerifyConnCode1.JPG

So let's go through the steps here:

1) Get a reference to the connection manager in question and its connection string

2) Try to acquire a connection to it

3) If the connection succeeds fire some OnInformation events that will be picked up by your logging mechanism.

4) If the connection fails fire some OnError events with some useful debugging information that will be picked up by your logging mechanism.

5) Er...that's it.

Dead easy isn't it? Wanna see it in action. Here's the output from a successful connection

20051010VerifyConnSuccessScr.JPG

You can see those custom log messages in the output window right? OK, how about if it fails:

20051010VerifyConnFailureScr.JPG

You can see its failed pretty easily so straightaway you can add an OnFailure precedence constraint to this task and do something to handle the error. You also get the custom error messages including the failed connection string so you can debug right here as soon as the package has finished executing.

What really makes this powerful is the ability to output your own custom error messages so you can see right where the problem lies. This is invaluable if you're using an expression on your connection string so you can see exactly what it has come up with!

You can take this a step further if you want and loop over all the connection managers in your package trying them out one by one. Here's the code to do that.

20051010VerifyConnCode2.JPG

One caveat with this though. If you've got alot of connection managers in your package then this could take some time so make sure you adjust the timeout property of your connection managers accordingly.

One important other thing you need to be aware of is this. Make sure you have DelayValidation=TRUE set on your tasks that use the connection managers otherwise your package will fail before it even reaches your verification script task.

So that's it! Comments are welcome. Do you think this is a good/bad/indifferent idea? Why do/don't you like it? Come on...I wanna know!

-Jamie

P.S. Have a go at running this for yourself if you like: http://blogs.conchango.com/Admin/ImageGallery/blogs.conchango.com/jamie.thomson/20051010VerifyDataSource.zip

Published 10 October 2005 20:04 by jamie.thomson

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

Professional Association for SQL Server (PASS) SIG said:

October 10, 2005 22:36
 

Alex said:

The script is great for database connections. However, it does not work for FTP or file connections. The script executed successfully even after I changed the password to the FTP site or remove the file from my directory. Is it possible to properly validate connections other than database connection?

Thanks
December 14, 2005 23:02
 

jamie.thomson said:

Hi Alex,
It would appear not - which is a shame. I guess for FTP it just verifies it can see the FTP Server. is that what you're tying to do?

For file connections I can kind of understand it too. Aquiring the conneciton probably only checks that it can get to the place where it *should* reside, regardless of whether it is there or not.

-Jamie
December 15, 2005 17:39
 

Rhonda said:

Thanks for the script.  Perfect for what I needed.  

February 27, 2007 18:29
 

geoff chovaz said:

For file connections you could always check if the file exists using the connection string as the parameter.

If System.IO.File.Exists(Dts.Connections.Item("FileConnection").ConnectionString) Then

...

BTW, GREAT blog. SSIS content is very helpful.

June 29, 2007 18:44
 

Francisco said:

Thanks for the nugget, very good ...

September 26, 2007 09:33
 

SSIS Noob said:

I managed to get this to work but I don't understand how to execute/exit the remainer of the package based on the success/failure of this code. When I run it, the task box changes to green indicating success but, "Now what?" How do I pass this status on to the next task? Can someone please exlpain to me how to perform these steps?

October 31, 2007 15:29
 

SSIS Noob said:

Actually ignore my previous comment, I figured it out. What I need help with is:

1)How to set DelayValidation=TRUE. I can't find this.

2)How to" ... get the custom error messages including the failed connection string so you can debug right here as soon as the package has finished executing."

October 31, 2007 15:35
 

James said:

Pease help me better understand this connection checking issue.

Does not a SSIS package, when executed, go through a validation phase where every connection is checked?

I've tried to run a SSIS package that uses an ODBC connection, and whenever I alter the ODBC data source (put a wrong password for instance) the package fails at validation step.

November 2, 2007 00:21
 

Duncan said:

James, i'm no expert but on execution SSIS only seems to validate the Connection Managers actually referenced in tasks... if (and i think Jamie said this) you click on your Data Flow Tasks and set the DelayValidation property = TRUE then they arent validated on execution and the package only fails when it reaches that task.

November 2, 2007 15:43
 

igorblackbelt said:

What I need to do:

I have a table with all the servers on our environment, I take this list and pass to a foreach loop container that will use the these server names for another task.

Is it possible to check the connections to each server (before I connect to the server) from my table instead of checking the connection managers?

April 1, 2008 19:48

Leave a Comment

(required) 
(optional)
(required) 
Submit

This Blog

Syndication

News

Powered by Community Server (Personal Edition), by Telligent Systems