Tech Note 1007 - Wonderware Application Server Scripting Implementations Part 1 Inserting A DB Row During Each Scan Cycle
Tech Note 1007 - Wonderware Application Server Scripting Implementations Part 1 Inserting A DB Row During Each Scan Cycle
All Tech Notes, Tech Alerts and KBCD documents and software are provided "as is" without warranty of any kind. See the Terms of Use for more information.
Topic#: 002839
Created: January 2014
Introduction
Scripting is one of the most powerful features in the Application Server world. This Tech Note will demonstrate
The approach that can insert one data row into a database table during each AppEngine's scan cycle.
Effective use of ArchestrA Objects' predefined attributes in the script's Expression for special requests.
Application Versions
Wonderware Application Server 2012 and later
From examining each Scheduler's attribute, Scheduler.ScanCyclesCnt is the attribute we can use if we set the trigger at every
CHANGING point. This analysis brings the following screenshot which shows the script Trigger Conditions.
As the result, we see the DB row is indeed inserted during each Scan Cycle.
Implementation Details
The following steps show the detail implementation of inserting a DB row during each Scan Cycle.
1. Define attributes in the UDA. For this example, we define three UDAs with string type:
• ConnectStringUDA
• DatabaseName with default value ScriptInsert
• UDAValue
2. Create a script called InsertDB, and define the following variables in the script's Declarations pane.
3. In the Script's OnScan pane, use the following code snippet for creating a database connection.
nodeName = System.Environment.MachineName;
sqlConnString = "Data Source = " + nodeName + ";Initial Catalog=" + me.DatabaseName + ";Integrated Security=true";
Me.ConnectStringUDA = sqlConnString;
LogMessage("sqlConnString: " + sqlConnString);
aaDBConn = aaDBAccess.CreateConnection(sqlConnString);
loopCount = 1;
Important Note
As one of the best practices in AppServer’s Scripting, we strongly suggest that you put the above statement,
aaDBAccess.CreateConnection, into the OnScan or Startup Execution type no matter in which situation. In other words,
you only need to create the database connection once and use it in the Execute Execution type.
Please keep in mind that creating a database connection is always expensive. This is because
Creating a DB connection uses a substantial amount of memory on both the database server and database client
machines.
Establishing a DB connection takes multiple network round trips to and from the database server.
Opening numerous connections can contribute to out-of-memory conditions, which might cause paging of memory to
disk and, thus, overall performance degradation.
4. In the Script's Execute pane, use the following code snippet to insert data into database in each AppEngine's Scan Cycle.
loopCount = loopCount + 1;
Me.UDAValu = "String Value: " + loopCount;
5. In the Script's OffScan pane, use the following code snippet to shut down the database connection.
aaDBAccess.Shutdown();
Startup: Scripts are called when the object containing the scripts is loaded into memory, such as during deployment, platform or
engine start.
OnScan: Scripts are called the first time an AppEngine calls this object to execute after the object's scan state changes to
OnScan. The OnScan method initiates local object attribute values or provides more flexibility in the creation of .NET or COM
objects.
Execute: Scripts are called each time the AppEngine performs a scan and the object is OnScan.
OffScan: Scripts are called when the object is taken OffScan. This script type is primarily used to clean up the object and account
for any needs to address as a result of the object no longer executing.
Shutdown: Scripts are primarily used to destroy COM objects and .NET objects, and to free memory.
References
file:///C|/inetpub/wwwroot/t002839/t002839.htm[1/28/2014 8:55:54 AM]
Wonderware Application Server Scripting Implementations Part 1: Inserting a DB Row During Each Scan Cycle
B. Lin and E. Xu
Tech Notes are published occasionally by Wonderware Technical Support. Publisher: Invensys Systems, Inc., 26561 Rancho Parkway South, Lake Forest, CA 92630. There is also
technical information on our software products at Wonderware Technical Support.
Back to top
©2014 Invensys Systems, Inc. All rights reserved. No part of the material protected by this copyright may be reproduced or utilized in any form or by any means, electronic or
mechanical, including photocopying, recording, broadcasting, or by any information storage and retrieval system, without permission in writing from Invensys Systems, Inc.
Terms of Use.