Oracle Transaction Processing and example demo JDBC Operations Bulk Delete

Source: Internet
Author: User

Transactions

As the basic unit of logical processing, it is composed of one or more SQL statements for database operations . Of course, there is a good application for non-database operations, such as a restore point set on a computer.

The basic nature of the transaction is described in another article: SQL Transactions and example demonstrations


Oracle and SQL Server differ on transactions

transactions in SQL Server are generally divided into implicit transactions, explicit transactions, and autocommit transactions.


Automatic transactions: For SQL Server , when a client submits an SQL statement, SQL Server automatically starts a transaction, and for such a transaction, after executing the SQL Statement is automatically submitted after the


Show transactions: This is also a more common use of transactions, in fact, in essence, in the automatic transaction, add a begintran,conn.commit,end Tran.


Implicit transactions: When connection is required to open a transaction , the implicit transaction is the start of the default open transaction and the database connection. Of course, there is still a commit or rollback operation to follow.


Oracle 's transactions are not so rich, similar to SQL Server 's implicit transactions; no need to open conn and Begin, as long as the subsequent operations commit or rollback operation.


mechanism for transaction submission

Start by understanding some basic concepts,data buffer cache: A high-speed read-only cache that connects hard disk files and Oracle data operations.

SGA: After starting an Oracle instance, a piece of memory space is opened up in memory to hold the server's control information and data.

Data blocks: The basic unit of data storage.

When connected to a database,Oracle creates a separate process -shadow process for connected users, which accompanies the user's entire operation;


1. Check the data block

2. constructing the undo data Block

To roll the data back and forth

3. Generate Redo logs

Logs for re-operation are stored in the log buffer cache .

4.LGWR process Start, commit transaction and write all log files


Java transaction processing

Testdemo: Bulk Delete with interface PreparedStatement and Oracle transaction implemented in java.sql


Common methods:

Int[]executebatch():

A batch of commands is submitted to the database for execution, and an array of update counts is returned if all command execution succeeds.

voidsetString(int Parameterindex,
String x) :

Sets the specified parameter to the given Java String value. When this value is sent to the database, the driver converts it to a SQL VARCHAR or LongVarChar value.

Demo

[Java] View PlainCopyPrint?
  1. /** Delete a user-August 11, 2014 18:19:04
  2. * @userId array of user IDs
  3. */
  4. Publicboolean deleteuser (string[] userId) {
  5. A thread-safe mutable string
  6. stringbuffersb=new StringBuffer ();
  7. Sb.append ("Deletefrom t_user where user_id =?");
  8. connectionconn=null;
  9. PREPAREDSTATEMENTPSMT = null;
  10. Booleanflag=false;
  11. Conn=dbutil.getconnection ();
  12. try {
  13. Turn off auto-commit transactions
  14. Conn.setautocommit (false);
  15. Create a PreparedStatement object to send the parameterized SQL statement to the database.
  16. psmt= conn.preparestatement (sb.tostring ());
  17. Adds a set of parameters to the batch command for this PreparedStatement object.
  18. for (inti =0; i<userid.length;i++) {
  19. Psmt.setstring (1,userid[i].trim ());
  20. Psmt.addbatch ();
  21. }
  22. //Perform batch updates
  23. Psmt.executebatch ();
  24. //Statement execution complete, commit this transaction
  25. Conn.commit ();
  26. Flag=true;
  27. }catch (SQLException e) {
  28. TODO auto-generated Catch block
  29. E.printstacktrace ();
  30. try{
  31. Conn.rollback ();
  32. }catch (SQLException E1) {
  33. TODO auto-generated Catch block
  34. E1.printstacktrace ();
  35. }
  36. }
  37. Returnflag;
  38. }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.