Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java JDBC savePoint

Tags:

java

jdbc

Reading

http://download.oracle.com/javase/6/docs/api/java/sql/Savepoint.html

it is not well spelled out what savepoints are mapped from database perspective.

When savepoints are set, are the database changes made available to other transactions?

Thanks

like image 988
gliptak Avatar asked Sep 02 '11 17:09

gliptak


People also ask

What is JDBC savepoint in Java?

An SQL savepoint represents the state of data and schemas at a particular point in time within a unit of work. You can use SQL statements to set a savepoint, release a savepoint, and restore data and schemas to the state that the savepoint represents.

How do you create a savepoint?

A SAVEPOINT is a point in a transaction in which you can roll the transaction back to a certain point without rolling back the entire transaction. Syntax for Savepoint command: SAVEPOINT SAVEPOINT_NAME; This command is used only in the creation of SAVEPOINT among all the transactions.

What is savepoint give an example?

A SAVEPOINT is a marker within a transaction that allows for a partial rollback. As changes are made in a transaction, we can create SAVEPOINTs to mark different points within the transaction. If we encounter an error, we can rollback to a SAVEPOINT or all the way back to the beginning of the transaction.

Can we use savepoint after commit?

After a savepoint has been created, you can either continue processing, commit your work, roll back the entire transaction, or roll back to the savepoint.


2 Answers

A savepoint marks a point that the current transaction can roll back to. Instead of rolling all of its changes back, it can choose to roll back only some of them. For example, suppose you:

  • start a transaction,
  • insert 10 rows into a table,
  • set a savepoint,
  • insert another 5 rows,
  • rollback to the savepoint,
  • commit the transaction.

After doing this, the table will contain the first 10 rows you inserted. The other 5 rows will have been deleted by the rollback.

Setting a savepoint doesn't 'save' any data to the database. It doesn't make database changes visible to any other transaction. A savepoint is just a marker that the current transaction can roll back to.

like image 87
Luke Woodward Avatar answered Oct 07 '22 22:10

Luke Woodward


Savepoints are not a JDBC feature, they are a DBMS feature.

In addition to Luke's detailed answer you might also want to read up on what the DBMS manuals explain about savepoints

http://www.postgresql.org/docs/current/static/sql-savepoint.html
http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/statements_10001.htm#BABFIJGC
http://publib.boulder.ibm.com/infocenter/db2luw/v9r7/topic/com.ibm.db2.luw.sql.ref.doc/doc/r0003271.html
http://msdn.microsoft.com/en-us/library/ms188378.aspx
http://dev.mysql.com/doc/refman/5.5/en/savepoint.html

like image 30
a_horse_with_no_name Avatar answered Oct 07 '22 20:10

a_horse_with_no_name