Sql what is a transaction




















Row locks obtained after this savepoint are released, as the statements executed after the savepoint have been rolled back completely. You can name a transaction, using a simple and memorable text string. This name is a reminder of what the transaction is about. Transaction names replace commit comments for distributed transactions, with the following advantages:. It is easier to monitor long-running transactions and to resolve in-doubt distributed transactions. You can view transaction names along with transaction IDs in applications.

For example, a database administrator can view transaction names in Enterprise Manager when monitoring system activity. Transaction names are written to the transaction auditing redo record, if compatibility is set to Oracle9 i or higher. LogMiner can use transaction names to search for a specific transaction from transaction auditing records in the redo log. When you name a transaction, you associate the transaction's name with its ID. Transaction names do not have to be unique; different transactions can have the same transaction name at the same time by the same owner.

You can use any name that enables you to distinguish the transaction. In previous releases, you could associate a comment with a transaction by using a commit comment. However, a comment can be associated with a transaction only when a transaction is being committed. Commit comments are still supported for backward compatibility. However, Oracle strongly recommends that you use transaction names. Commit comments are ignored in named transactions.

Oracle Database Administrator's Guide for more information about distributed transactions. In a distributed database, Oracle must coordinate transaction control over a network and maintain data consistency, even if a network or system failure occurs.

A distributed transaction is a transaction that includes one or more statements that update data on two or more distinct nodes of a distributed database. A two-phase commit mechanism guarantees that all database servers participating in a distributed transaction either all commit or all undo the statements in the transaction. A two-phase commit mechanism also protects implicit DML operations performed by integrity constraints, remote procedure calls, and triggers.

The Oracle two-phase commit mechanism is completely transparent to users who issue distributed transactions. In fact, users need not even know the transaction is distributed. A COMMIT statement denoting the end of a transaction automatically triggers the two-phase commit mechanism to commit the transaction. No coding or complex statement syntax is required to include distributed transactions within the body of a database application. The recoverer RECO background process automatically resolves the outcome of in-doubt distributed transactions —distributed transactions in which the commit was interrupted by any type of system or network failure.

After the failure is repaired and communication is reestablished, the RECO process of each local Oracle database automatically commits or rolls back any in-doubt distributed transactions consistently on all involved nodes.

In the event of a long-term failure, Oracle allows each local administrator to manually commit or undo any distributed transactions that are in doubt as a result of the failure. This option enables the local database administrator to free any locked resources that are held indefinitely as a result of the long-term failure. If a database must be recovered to a point in the past, Oracle's recovery facilities enable database administrators at other sites to return their databases to the earlier point in time also.

This operation ensures that the global database remains consistent. Autonomous transactions are independent transactions that can be called from within another transaction. An autonomous transaction lets you leave the context of the calling transaction, perform some SQL operations, commit or undo those operations, and then return to the calling transaction's context and continue with that transaction.

Once invoked, an autonomous transaction is totally independent of the main transaction that called it. It does not see any of the uncommitted changes made by the main transaction and does not share any locks or resources with the main transaction. Changes made by an autonomous transaction become visible to other transactions upon commit of the autonomous transactions. One autonomous transaction can call another. There are no limits, other than resource limits, on how many levels of autonomous transactions can be called.

Deadlocks are possible between an autonomous transaction and its calling transaction. Oracle detects such deadlocks and returns an error. The application developer is responsible for avoiding deadlock situations. Autonomous transactions are useful for implementing actions that need to be performed independently, regardless of whether the calling transaction commits or rolls back, such as transaction logging and retry counters.

A pragma is a compiler directive. This operation ensures that SQL operations performed in this block or other blocks called from it have no dependence or effect on the state of the caller's transaction context. When an autonomous block invokes another autonomous block or itself, the called block does not share any transaction context with the calling block. However, when an autonomous block invokes a non-autonomous block that is, one that is not declared to be autonomous , the called block inherits the transaction context of the calling autonomous block.

Examples of such statements are:. Similarly, transaction control statements in the main transaction apply only to that transaction and not to any autonomous transaction that it calls. For example, rolling back the main transaction to a savepoint taken before the beginning of an autonomous transaction does not undo the autonomous transaction.

This chapter contains the following topics: Introduction to Transactions Overview of Transaction Management Overview of Autonomous Transactions Introduction to Transactions A transaction is a logical unit of work that contains one or more SQL statements. When a bank customer transfers money from a savings account to a checking account, the transaction can consist of three separate operations: Decrement the savings account Increment the checking account Record the transaction in the transaction journal Oracle must allow for two situations.

Figure illustrates the banking transaction example. Executing successfully means that a single statement was: Parsed Found to be a valid SQL construction Run without error as an atomic unit.

The mark is placed in the transaction log only if the database is updated by the marked transaction. Transactions that do not modify data are not marked.

In the following example, M2 is the name of the mark. When nesting transactions, trying to mark a transaction that is already marked results in a warning not error message:. The following example shows the effect of rolling back a transaction.

The following example shows how to mark a transaction. The transaction CandidateDelete is marked. Feedback will be sent to Microsoft: By pressing the submit button, your feedback will be used to improve Microsoft products and services.

Privacy policy. Skip to main content. This browser is no longer supported. Download Microsoft Edge More info. Prior to SQL Server , explicit transactions were also called user-defined or user-specified transactions. Explicit transaction mode lasts only for the duration of the transaction. When the transaction ends, the connection returns to the transaction mode it was in before the explicit transaction was started.

This occurs because, by default, the connection is in autocommit transaction mode. If you want no changes to be committed unless you explicitly indicate so, you need to set the connection to implicit transaction mode.

In case neither of these commands are issued, the transaction will be automatically rolled back when the user disconnects. This is why it is not a best practice to use implicit transaction mode on a highly concurrent database. NET to take advantage of SQL Server 's capability of having multiple active commands on a single connection object.

When MARS is enabled, you can have multiple interleaved batches executing at the same time, so all the changes made to the execution environment are scoped to the specific batch until the execution of the batch is complete.

Once the execution of the batch completes, the execution settings are copied to the default environment. Thus a connection is said to be using batch-scoped transaction mode if it is running a transaction, has MARS enabled on it, and has multiple batches running at the same time. MARS allows executing multiple interleaved batches of commands.

Distributed Transactions in SQL Server In contrast to local transactions that are restricted to a single resource or database, distributed transactions span two or more servers, that are known as resource managers. Transaction management needs to be coordinated among the resource managers via a server component known as a transaction manager or transaction coordinator.

A transaction with a single SQL Server that spans two or more databases is actually a distributed transaction. SQL Server, however, manages the distributed transaction internally. At the application level, a distributed transaction is managed in much the same way as a local transaction.

At the end of the transaction, the application requests the transaction to be either committed or rolled back. A distributed commit must be managed differently by the transaction manager to minimize the risk that a network failure might lead you to a situation when one of the resource managers is committing instead of rolling back the transactions due to failure caused by various reasons. This critical situation can be handled by managing the commit process in two phases, also known as a two-phase commit: Prepare phase: When the transaction manager receives a commit request, it sends a prepare command to all of the resource managers involved in the transaction.

Each resource manager then does everything required to make the transaction durable, and all buffers holding any of the log images for other transactions are flushed to disk.

As each resource manager completes the prepare phase, it returns success or failure of the prepare phase to the transaction manager. Commit phase: If the transaction manager receives successful prepares from all of the resource managers then it sends a COMMIT command to each resource manager.

If all of the resource managers report a successful commit then the transaction manager sends a notification of success to the application. If any resource manager reports a failure to prepare, the transaction manager sends a ROLLBACK statement to each resource manager and indicates the failure of the commit to the application. Guidelines to Code Efficient Transactions We recommend you use the following guidelines while coding transactions to make them as efficient as possible: Do not require input from users during a transaction.

Get all required input from users before a transaction is started. If additional user input is required during a transaction then roll back the current transaction and restart the transaction after the user input is supplied. Even if users respond immediately, human reaction times are vastly slower than computer speeds.

All resources held by the transaction are held for an extremely long time, that has the potential to cause blocking problems. If users do not respond then the transaction remains active, locking critical resources until they respond, that may not happen for several minutes or even hours. Do not open a transaction while browsing through data, if at all possible. Transactions should not be started until all preliminary data analysis has been completed.

Keep the transaction as short as possible. After you know the modifications that need to be made, start a transaction, execute the modification statements, and then immediately commit or roll back.

Do not open the transaction before it is required. Make intelligent use of lower cursor concurrency options, such as optimistic concurrency options. In a system with a low probability of concurrent updates, the overhead of dealing with an occasional "somebody else changed your data after you read it" error can be much lower than the overhead of always locking rows as they are read.

Access the least amount of data possible while in a transaction. The smaller the amount of data that you access in the transaction, the fewer the number of rows that will be locked, reducing contention between transactions. It signals the database to save the work. Transactions end on explicit or implicit commits and rollbacks. It's an intentionally artificial example but representative of transaction processing fundamentals.

It keeps things simple so you can focus on the important issue of what can happen in a transaction. The example runs by itself, so you don't need to be concerned with this here, but you should always consider whether it's a potential issue. Try It Out: Creating a Parent-Child relationship Before you code the transaction let's create two tables to apply our transaction code and understand the transaction concept.

Enter the following SQL statement to create tables with a primary-key and foreign-key, in other words a parent-child relationship.

The Person table will have a primary-key column that will be referenced by the PersonDetails table via a foreign key column as shown below here. Listing And it should show the status as "Command s completed successfully" as shown in Image below. Executing the Create table statement parent-child relationship Next let's insert some data into the Person and PersonDetails table, by executing the statement below, and click "Execute". Since a child can have only those records that map to the parent, hence we can only insert child records into PersonDetails for those PersonIDs that are already available in the Person table.

So now we have a perfect parent-child relationship, where we have two parent records and 2 matching child records in the Person and PersonDetails tables respectively as shown in Figure below: Figure In other words only unique values are allowed. Also, the last column Company allows null values. Similarly, the PersonDetails table is a foreign-key or child table, it has a PersonID column that is a foreign-key column and reference to Person.

It also has an Address column. The child or foreign-key table can only have those records that has a matching Primary-key column value available in the Parent or Primary-key table as shown in Figure above, if a child record is inserted that doesn't have a matching parent or primary ley value then it will result in an error and not insterted into the child table.

Select the statement as shown in Figure , and then click "Execute" to run the query. You will see that the person named "Vamika" has been added to the table, as shown in the Results tab in Figure Row inserted in a transaction Add another person with the parameter values.

Enter the following statement and execute it as you've done previously with other similar statements.

You should see that person "Arshika" has been added to the Person table.



0コメント

  • 1000 / 1000