system.db.beginNamedQueryTransaction
This function is used in Python Scripting.
Description​
Begins a new database transaction using Named Queries. Database transactions are used to execute multiple queries in an atomic fashion. After executing queries, you must either commit the transaction to have your changes take effect or rollback the transaction, which will make all operations since the last commit not take place. The transaction is given a new unique string code, which is then returned. You can then use this code as the tx argument for other system.db.* function calls to execute various types of queries using this transaction.
An open transaction consumes one database connection until it is closed. Because leaving connections open indefinitely would exhaust the connection pool, each transaction is given a timeout. Each time the transaction is used, the timeout timer is reset. For example, if you make a transaction with a timeout of one minute, you must complete that transaction within a minute. If a transaction is detected to have timed out, it will be automatically closed and its transaction id will no longer be valid.
Client Permission Restrictions​
This scripting function has no Client Permission restrictions.
Syntax - Vision​
system.db.beginNamedQueryTransaction([database], [isolationLevel], [timeout])
Parameters​
Type | Parameter | Description |
---|---|---|
String | database | The name of the database connection to create a transaction in. If omitted, uses the project's default connection. |
Integer | isolationLevel | The transaction isolation level to use. Use one of the four constants: system.db.READ_COMMITTED , system.db.READ_UNCOMMITTED , system.db.REPEATABLE_READ , or system.db.SERIALIZABLE . If omitted, uses system.db.READ_COMMITTED . [optional] |
Integer | timeout | The amount of time, in milliseconds, that this connection is allowed to remain open without being used. Timeout counter is reset any time a query or call is executed against the transaction, or when committed or rolled-back. If omitted, defaults to 30,000. [optional] |
Returns​
String - The new transaction ID. You'll use this ID as the "tx" argument for all other calls to have them execute against this transaction.
Scope​
Vision Client
Syntax - Perspective and Gateway​
system.db.beginNamedQueryTransaction(project, database, [isolationLevel], [timeout])
Parameters​
Type | Parameter | Description |
---|---|---|
String | project | The name of the project that contains the named query. |
String | database | The name of the database connection to create a transaction in. |
Integer | isolationLevel | The transaction isolation level to use. Use one of the four constants: system.db.READ_COMMITTED, system.db.READ_UNCOMMITTED, system.db.REPEATABLE_READ, or system.db.SERIALIZABLE. If omitted, uses system.db.READ_COMMITTED. [optional] |
Integer | timeout | The amount of time, in milliseconds, that this connection is allowed to remain open without being used. Timeout counter is reset any time a query or call is executed against the transaction, or when committed or rolled-back. If omitted, defaults to 30,000. [optional] |
Returns​
String - The new transaction ID. You'll use this ID as the "tx" argument for all other calls to have them execute against this transaction.
Scope​
Gateway, Perspective Session
Isolation Level Values​
The following table lists each value of the isolationLevel parameter and its associated level. Either the integer value or constant may be passed. Note that some JDBC drivers only support some levels, so the driver's documentation should be consulted. Isolation levels are well documented online, but the following link is a great starting point: Data Concurrency and Consistency
Isolation Level | Int Value | Constant |
---|---|---|
Read Uncommitted | 1 | system.db.READ_UNCOMMITTED |
Read Committed | 2 | system.db.READ_COMMITTED |
Repeatable Read | 4 | system.db.REPEATABLE_READ |
Serializable | 8 | system.db.SERIALIZABLE |
Code Examples​
# This example starts a transaction and checks a screen to see if the transaction should be completed or reversed (rolled back).
# The example assumes you have several components on screen, including a Checkbox and two input components, and a Named Query that takes in an ID and a string value.
# Get details from the screen: Numeric Text Field, Text Field, Checkbox
idEntry = event.source.parent.getComponent('ID Field').intValue
valueEntry = event.source.parent.getComponent('Value Field').text
shouldRollback = event.source.parent.getComponent('CheckBox').selected
# Begin the transaction.
datasource = "MYSQL"
isolationLevel = system.db.READ_COMMITTED
timeout = 60000
txNumber = system.db.beginNamedQueryTransaction(datasource, isolationLevel, timeout)
# Start by running a Named Query against the transaction.
namedQueryPath = "InsertQueries/AddValues"
params = {"id":idEntry, "value":valueEntry}
system.db.runNamedQuery(namedQueryPath, params, txNumber)
# Check the window to see if the user selected to cancel the transaction.
if shouldRollback:
# cancel the transaction
system.db.rollbackTransaction(txNumber)
print "Transaction rolled back"
else:
# complete the transaction
system.db.commitTransaction(txNumber)
print "Transaction committed"
# Close the transaction now that we are done.
system.db.closeTransaction(txNumber)