How can you start a database transaction in the database?
-
O (a) By asking a Transaction object to your Connection, and calling the method begin() on it
-
O (b) By asking a Transaction object to your Connection, and setting the autoCommit property
-
O (c) By calling the method beginTransaction() on the Connection object
-
O (d) By setting the autoCommit property of the Connection to false, and execute a statement
In JDBC, a transaction begins when you disable auto-commit by calling Connection.setAutoCommit(false). This shifts from single-statement auto-committing mode to manual transaction control. You then execute your SQL statements, and complete the transaction by calling commit() to apply changes or rollback() to cancel them. There is no beginTransaction() method in the JDBC Connection API.
To start a database transaction in the database, the correct option is D) By setting the autoCommit property of the Connection to false, and execute a statement.
Explanation for each option:
Option A) By asking a Transaction object to your Connection, and calling the method begin() on it - This option is incorrect. While it is possible to start a transaction using a Transaction object, typically, the transaction is managed directly through the Connection object.
Option B) By asking a Transaction object to your Connection, and setting the autoCommit property - This option is incorrect. The autoCommit property determines whether each SQL statement is automatically committed or not. It does not directly start a transaction.
Option C) By calling the method beginTransaction() on the Connection object - This option is incorrect. While some databases provide a method called beginTransaction() to start a transaction, it is not a standard method in the JDBC API.
Option D) By setting the autoCommit property of the Connection to false, and execute a statement - This option is correct. To start a transaction, you can set the autoCommit property of the Connection to false. This means that each SQL statement will not be automatically committed, allowing you to group multiple statements into a single transaction. You can then execute the desired statements to perform your transaction.