Viewing the properties of connection Retainsameconnection, the default value is False, which means that each task will use the connection separately, and each task will open and close the connection separately. Modify this property to True, and all tasks will use the same connection.
This property setting is important when you need to connect to a database in a recurring task, which avoids opening multiple times and closing the connection. When the package starts executing, when the connection,package ends, turn off connection, ensuring that all tasks use the same connection.
Example 1, using SQL's Begin/commit/rollback tran to commit or rollback a transaction
Control flow for the package
To create a sample table
Create Table int)
The SQL statement for the EXEC SQL Statment task is
Insert into dbo.dt_test Values (1) Insert into dbo.dt_test Values (' a ')
If Retainsameconnection is false, then execution will be error-
The cause of the error can be viewed from the Progess tab
Task EXEC SQL statment error reason, it is clear that the data type of the inserted data is not
[Execute SQL Task] Error:executing the query "insert into dbo.dt_test values (' a ')
Insert INTO D ... "failed with the following error:" Conversion failed when converting the varchar value ' a ' to data type I NT. ". Possible failure Reasons:problems with the query, ' ResultSet ' property not set correctly, parameters not set correctly, O R connection not established correctly.
The cause of the error for task rollback is: There is no BEGIN TRAN clause, and since retainsameconnection is false, each task is opened and closed separately connection, so the task There is no BEGIN TRAN clause in rollback.
[Execute SQL Task] Error:executing the query "Rollback TRAN" failed with the following error: "The rollback TRANSACTION request have no Corre Sponding BEGIN TRANSACTION. " Possible failure Reasons:problems with the query, ' ResultSet ' property not set correctly, parameters not set correctly, O R connection not established correctly.
Set Retainsameconnection to True so that all task connection are the same. To execute again or fail, the reason for the error is:
[Execute SQL Task] Error:executing the query "Rollback TRAN" failed with the following error: "The rollback TRANSACTION request have no Corre Sponding BEGIN TRANSACTION. " Possible failure Reasons:problems with the query, ' ResultSet ' property not set correctly, parameters not set correctly, O R connection not established correctly.
The bold guess is that the Execute SQL Task automatically rolls back the transaction when it fails. Modify the package, add a task Insert Statement, and execute the SQL statement:
INSERT INTO dbo.dt_testvalues (0)
and add breakpoint on the Exec SQL statement task to trigger the breakpoint before executing the task.
Re-execute Package, at Breakpoint, view table dt_test,0 insert INTO table
Continue execution, view table Dt_test, data has been rolled back.
Conclusion 1: When Execute SQL task execution fails, the transaction is automatically rolled back, but when execute SQL task executes successfully, the transaction is not committed, the display opens a transaction, and a commit is required to be displayed.
Conclusion 2: When the package execution is finished, the package rolls back the uncommitted transactions.
Modify the exec SQL Statment to modify the executed SQL to the correct SQL statement, and the Commit Tran task will commit the transaction.
INSERT INTO Dbo.dt_testvalues (1)
After the commit Tran task Disable,package is executed, the package detects uncommitted transactions and the package rolls back those uncommitted transactions.
Conclusion 3: When connection is closed, the package rolls back uncommitted transactions.
The Connetion property Retainsameconnection is set to false, and its rollback scope is different.
Modifying the SQL statement executed by the BEGIN TRAN task, the package performs a rollback of the statement of the Begin TRAN task without rolling back the INSERT statement and EXEC statement.
begin Traninsert into dbo.dt_test values (3)
There is a significant drawback to using Connetion's property retainsameconnection for transactional processing, which is that SSIS often needs to process data from multiple databases under the same connection, so for transactions across databases, This approach is powerless, with SSIS's own MSDTC (Microsoft Distributed Transaction Service) capable of handling cross-database transactions.
One of the transaction handling of the package: using Connetion properties Retainsameconnection