On very rare occasions, you will find that you need to create a BPM script using a Java-based vuser type instead of a C-based virtual user type. There is one nasty gotcha to keep in mind if you need to do this.
Here is a partial code snippet from a BPM script for an that emulates a program that connects to a database, and runs some queries.
public int init() throws Throwable {
// Print user and environment details
lr.output_message("$$ Server: " + lr.eval_string(""));
lr.output_message("$$ Database: " + lr.eval_string(""));
lr.output_message("$$ Account: " + lr.eval_string(""));
lr.output_message("$$ Java Version: " + System.getProperty("java.version"));
lr.start_transaction("login");
// Connect to database
_oracledatasource1 = new oracle.jdbc.pool.OracleDataSource();
_oracledatasource1.setDriverType("thin");
_oracledatasource1.setLoginTimeout(5);
_oracledatasource1.setUser(lr.eval_string(""));
_oracledatasource1.setPassword(lr.eval_string(""));
_oracledatasource1.setServerName(lr.eval_string(""));
_oracledatasource1.setPortNumber(1521);
_oracledatasource1.setDatabaseName(lr.eval_string(""));
_string1 = _oracledatasource1.getURL();
_connection1 = _oracledatasource1.getConnection();
// Print account expiry date
_oraclestatement1 = new oracle.jdbc.driver.OracleStatement((oracle.jdbc.driver.OracleConnection)_connection1, 1, 10);
_resultset1 = ((oracle.jdbc.driver.OracleStatement)_oraclestatement1).executeQuery("SELECT EXPIRY_DATE, SYSDATE FROM USER_USERS");
_resultset1.next();
lr.output_message("$$ User account expires on: " + _resultset1.getString(1));
lr.end_transaction("login", lr.AUTO);
return 0;
}
At a glance, the code looks simple, correct and robust. It even prints some helpful troubleshooting information to the log.
Unfortunately, if the getConnection() method throws an exception, then BAC will not fail the login transaction (because it never gets to the end_transaction statement), instead it will show that the script is just not running. Of course this does not trigger an alert because the transaction is listed as "no run", rather than "fail". This is an extremely dangerous situation, as the BPM script will not alert anyone if the application becomes unavailable.
Here is a screenshot of the BAC Transaction Analysis page for a profile that had this problem...
The solution to this problem is to wrap method calls that can throw exceptions in a try/catch block, and ensure that the transaction is ended with "fail" status.
To ensure that each transaction is ended with fail status if an exception is thrown during the transaction steps, it seems best to wrap all steps in a transaction within a single try/catch block, with separate try/catch blocks for each transaction.
Here is the modified code...
public int init() throws Throwable {
try {
// Print user and environment details
lr.output_message("$$ Server: " + lr.eval_string(""));
lr.output_message("$$ Database: " + lr.eval_string(""));
lr.output_message("$$ Account: " + lr.eval_string(""));
lr.output_message("$$ Java Version: " + System.getProperty("java.version"));
lr.start_transaction("log in");
// Oracle connection details
_oracledatasource1 = new oracle.jdbc.pool.OracleDataSource();
_oracledatasource1.setDriverType("thin");
_oracledatasource1.setLoginTimeout(5);
_oracledatasource1.setUser(lr.eval_string(""));
_oracledatasource1.setPassword(lr.eval_string(""));
_oracledatasource1.setServerName(lr.eval_string(""));
_oracledatasource1.setPortNumber(1521);
_oracledatasource1.setDatabaseName(lr.eval_string(""));
_string1 = _oracledatasource1.getURL();
_connection1 = _oracledatasource1.getConnection();
// Print account expiry date
_oraclestatement1 = new oracle.jdbc.driver.OracleStatement((oracle.jdbc.driver.OracleConnection)_connection1, 1, 10);
_resultset1 = ((oracle.jdbc.driver.OracleStatement)_oraclestatement1).executeQuery("SELECT EXPIRY_DATE, SYSDATE FROM USER_USERS");
_resultset1.next();
lr.output_message("$$ Account " + lr.eval_string("") + "expires on: " + _resultset1.getString(1));
lr.end_transaction("log in", lr.AUTO);
} catch (Exception e) {
// Print exception description, fail transaction, close connections, and rethrow exception.
lr.error_message("$$ Exception thrown: " + e.toString());
lr.end_transaction("log in", lr.FAIL);
if (_connection1 != null) {
_connection1.close(); // Close Connection object if it was successfully opened.
}
throw e;
}
return 0;
}
Note that this advice only really applies for methods that are not VuGen specific e.g. if it was a web.url() or web.submit_data() method that failed, then the error will be detected, and the transaction will be marked as failed without adding try/catch blocks.
Tech tips from JDS

Browser Console
Read More

Glide Variables
Read More

Understanding Database Indexes in ServiceNow
Read More

Fast-track ServiceNow upgrades with Automated Testing Framework (ATF)
Read More

Read More

Splunk .conf18
Read More

ServiceNow Catalog Client Scripts: G_Form Clear Values
Read More

Is DevPerfOps a thing?
Read More

The benefits of performance testing with LoadRunner
Read More

Monitoring Atlassian Suite with AppDynamics
Read More

5 quick tips for customising your SAP data in Splunk
Read More

How to maintain versatility throughout your SAP lifecycle
Read More

How to revitalise your performance testing in SAP
Read More

Reserve and import data through Micro Focus ALM
Read More

How to effectively manage your CMDB in ServiceNow
Read More

ServiceNow and single sign-on
Read More

How to customise the ServiceNow Service Portal
Read More

Integrating a hand-signed signature to an Incident Form in ServiceNow
Read More

Integrating OMi (Operations Manager i) with ServiceNow
Read More

Implementing an electronic signature in ALM
Read More

Service portal simplicity
Read More

Learning from real-world cloud security crises
Read More

Static Variables and Pointers in ServiceNow
Read More

Citrix and web client engagement on an Enterprise system
Read More

Understanding outbound web services in ServiceNow
Read More

How to solve SSL 3 recording issues in HPE VuGen
Read More

How to record Angular JS Single Page Applications (SPA)
Read More

Calculating Pacing for Performance Tests
Read More

Vugen and GitHub Integration
Read More

What’s new in LoadRunner 12.53
Read More

Filtered Reference Fields in ServiceNow
Read More

ServiceNow performance testing tips
Read More

Monitor Dell Foglight Topology Churn with Splunk
Read More

Straight-Through Processing with ServiceNow
Read More

Splunk: Using Regex to Simplify Your Data
Read More

ServiceNow Choice List Dependencies
Read More

Tips for replaying RDP VuGen scripts in BSM or LoadRunner
Read More

Incorporating iSPI metric reports into MyBSM dashboard pages
Read More

Using SV contexts to simulate stored data
Read More

What’s new in LoadRunner 12.02
Read More

Recycle Bin for Quality Center
Read More

LoadRunner Correlation with web_reg_save_param_regexp
Read More

LoadRunner 11.52
Read More

QC for Testers – Quiz
Read More

Agile Performance Tuning with HP Diagnostics
Read More

What’s new in HP Service Virtualization 2.30
Read More

Understanding LoadRunner Virtual User Days (VUDs)
Read More

Problems recording HTTPS with VuGen
Read More

Improving the management and efficiency of QTP execution
Read More

Performance testing Oracle WebCenter with LoadRunner
Read More

Generating custom reports with Quality Center OTA using Python
Read More

Asynchronous Communication: Scripting For Cognos
Read More

How to fix common VuGen recording problems
Read More

Monitoring Active Directory accounts with HP BAC
Read More

URL Attachments in Quality Center
Read More

What’s new in LoadRunner 11.00?
Read More

Restore old License Usage stats after upgrading Quality Center
Read More

Changing LoadRunner/VuGen log options at runtime
Read More

Restricting large attachments in Quality Center
Read More

Retrieving Quality Center user login statistics
Read More

A comparison of open source load testing tools
...
Read More

Worst practices in performance testing
Read More

LoadRunner Sales Questions
Read More

LoadRunner Analysis: Hints and tips
Read More

LoadRunner in Windows 7
HP Loadrunner 11 is now available. This new version now natively supports Windows 7 and Windows Server 2008. I ...
Read More

Using the QuickTest Professional “commuter” license
Read More

Installing HP Diagnostics
Read More

Understanding LoadRunner licensing
Read More

VuGen scripting for YouTube video
Read More

Creating a Web + MMS vuser
Read More

Why you should use backwards dates
Read More

How to get the host’s IP address from within VuGen
Read More

VuGen scripting for BMC Remedy Action Request System 7.1
Read More

Unique usernames for BPM scripts
Read More

Mapping drives for LoadRunner Windows monitoring
Read More

VuGen feature requests
Read More

LoadRunner script completion checklist
Read More

Querying Quality Center user roles
Read More

Querying the Quality Center Database
Read More

HPSU 2009 Presentation – Performance Testing Web 2.0
Read More

Scaling HP Diagnostics
Read More

Global variables aren’t really global in LoadRunner
Read More

Client-side certificates for VuGen
Read More

Detect malicious HTML/JavaScript payloads with WebInspect (e.g. ASPROX, Gumblar, Income Iframe)
Read More

VuGen code snippets
Read More

Integrating QTP with Terminal Emulators
Read More

Why you must add try/catch blocks to Java-based BPM scripts
Read More

Querying a MySQL database with LoadRunner
Read More

ANZTB 2009 Presentation: Performance Testing Web 2.0
Read More

How to make QTP “analog mode” steps more reliable
Read More

Testing multiple browsers in a Standardized Operating Environment (SOE)
Read More

DNS-based load balancing for virtual users
Read More

What’s new in LoadRunner 9.50?
Read More

Calculating the difference between two dates or timestamps
Read More

The “is it done yet” loop
Read More

Think time that cannot be ignored
Read More

Understanding aggregate variance within LoadRunner analysis
Read More

Load balancing vusers without a load balancer
Read More

Harvesting file names with VuGen
Read More

Parameterising Unix/Posix timestamps in VuGen
Read More

HP Software trial license periods
Read More

How to handle HTTP POSTs with a changing number of name-value pairs
Read More

VuGen string comparison behaviour
Read More

Persistent data in VuGen with MySQL
Read More

How to write a Performance Test Plan
Read More

Unable to add virtual machine
To get ...
Read More

LoadRunner scripting languages
Read More

WDiff replacement for VuGen
Read More

Testing web services with a standard Web Vuser
Read More

Why your BPM scripts should use Download Filters
Read More

Querying your web server logs
Read More

Importing IIS Logs into SQL Server
Read More

QTP “Uninstall was not completed” problem
Read More

VuGen correlation for SAP Web Dynpro
Read More

How to save $500 on your HP software license
Read More

Testing and monitoring acronyms
Read More

Solving VuGen script generation errors
Read More

An introduction to SiteScope EMS Topology
Read More

Using the BAC JMX Console
Read More
Obviously if the method had a checked exception, then the compiler would force you to put it inside a try/catch block.
In this case, SQLException is not a checked exception.
Also, it is not really necessary to add try/catch blocks to a VuGen script that will be used with LoadRunner, because if a vuser throws an exception and exits, it will be obvious from seeing a stopped vuser in the LoadRunner Controller window.