search
HomeDatabaseMysql TutorialOracle事务处理及实例演示JDBC操作批量删除

作为逻辑处理的基本单位,对于数据库操作来说由一条或者多条sql语句来构成。当然还有针对非数据库操作的,如在计算机中设置的还原

事务

作为逻辑处理的基本单位,对于数据库操作来说由一条或者多条sql语句来构成。当然还有针对非数据库操作的,如在计算机中设置的还原点即是一个很好的应用。

对于事务的基本性质在另一篇中有所叙述:SQL 事务及实例演示

Oracle和sql server在事务上区别

sql server中的事务一般分为隐式事务、显式事务、自动提交事务。

自动事务:对于sql server来说,当客户端提交一条sql语句时,这时候sql server都会自动启动一个事务;对于这样的事务,在执行完sql语句后会自动提交。

显示事务:这也是比较常见的使用的事务;其实实质上也就是在自动事务上,增加一个Begintran,conn.commit,end tran.

隐式事务:相比显示事务需要开启connection,,隐式事务是默认开启事务的Begin和数据库连接的。当然后面还是要进行commit或rollback操作的。

而oracle的事务就没有这么如此丰富了,和sql server的隐式事务类似;无需开启conn和Begin,只要在后续操作进行commit或rollback操作。

事务提交的机制

先了解一些基础的概念,data buffer cache:相当于一块连接硬盘文件和oracle数据操作的高速只读缓存。

SGA:在启动oracle实例后,在内存中开辟的一块内存空间,用于存放服务器的控制信息以及数据。

数据块:数据存储的基本单位。

当连接到数据库后,oracle为连接的用户创建独立的进程-影子进程,这个进程伴随用户的整个操作;

1.检查数据块

2.构造undo数据块

以此来回滚数据

3.生成redo日志

用于重新操作的日志存放在log buffer cache中。

4.lgwr进程启动,提交事务并书写所有日志文件

java事务处理

TestDemo:结合位于java.sql下面的接口 PreparedStatement和oracle事务实现批量删除

常用方法:

int[]executeBatch():

将一批命令提交给数据库来执行,如果全部命令执行成功,则返回更新计数组成的数组。

voidsetString(int parameterIndex,

String x):

将指定参数设置为给定Java String 值。在将此值发送给数据库时,驱动程序将它转换成一个 SQL VARCHAR 或 LONGVARCHAR 值。

Demo

/**删除用户-2014年8月11日18:19:04
 * @userId 用户id数组
 */
publicBoolean DeleteUser(String[] userId){
//一个线程安全的可变字符串
StringBuffersb=new StringBuffer();
sb.append("deletefrom t_user where user_id =?");
Connectionconn=null;
PreparedStatementpsmt = null;
Booleanflag=false;
 
conn=DButil.getConnection();
try {
//关闭自动提交事务
conn.setAutoCommit(false);
//创建一个 PreparedStatement 对象来将参数化的 SQL语句发送到数据库。
psmt= conn.prepareStatement(sb.toString());
//将一组参数添加到此 PreparedStatement 对象的批处理命令中。
for(inti =0 ;ipsmt.setString(1,userId[i].trim());
psmt.addBatch();                               
}
 
 
  // 执行批量更新
  psmt.executeBatch();
  // 语句执行完毕,提交本事务
  conn.commit();
 
  flag=true;
}catch (SQLException e) {
//TODO Auto-generated catch block
e.printStackTrace();
try{
conn.rollback();
}catch (SQLException e1) {
//TODO Auto-generated catch block
e1.printStackTrace();
}
}
 
returnflag;
}

总结

总的来说,感觉还是对oracle很多内部的机制了解的有限。前几天的设计模式讲课,也让想到这里的一些东西才有了这篇博客。只能说是非常浅显的认知,oracle在事务这里的处理相比在事务的处理上本质还是一致的。

Oracle 11g 在RedHat Linux 5.8_x64平台的安装手册

Linux-6-64下安装Oracle 12C笔记

在CentOS 6.4下安装Oracle 11gR2(x64)

Oracle 11gR2 在VMWare虚拟机中安装步骤

Debian 下 安装 Oracle 11g XE R2

本文永久更新链接地址:

linux

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How does MySQL index cardinality affect query performance?How does MySQL index cardinality affect query performance?Apr 14, 2025 am 12:18 AM

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

MySQL: Resources and Tutorials for New UsersMySQL: Resources and Tutorials for New UsersApr 14, 2025 am 12:16 AM

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

Real-World MySQL: Examples and Use CasesReal-World MySQL: Examples and Use CasesApr 14, 2025 am 12:15 AM

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.

SQL Commands in MySQL: Practical ExamplesSQL Commands in MySQL: Practical ExamplesApr 14, 2025 am 12:09 AM

SQL commands in MySQL can be divided into categories such as DDL, DML, DQL, DCL, etc., and are used to create, modify, delete databases and tables, insert, update, delete data, and perform complex query operations. 1. Basic usage includes CREATETABLE creation table, INSERTINTO insert data, and SELECT query data. 2. Advanced usage involves JOIN for table joins, subqueries and GROUPBY for data aggregation. 3. Common errors such as syntax errors, data type mismatch and permission problems can be debugged through syntax checking, data type conversion and permission management. 4. Performance optimization suggestions include using indexes, avoiding full table scanning, optimizing JOIN operations and using transactions to ensure data consistency.

How does InnoDB handle ACID compliance?How does InnoDB handle ACID compliance?Apr 14, 2025 am 12:03 AM

InnoDB achieves atomicity through undolog, consistency and isolation through locking mechanism and MVCC, and persistence through redolog. 1) Atomicity: Use undolog to record the original data to ensure that the transaction can be rolled back. 2) Consistency: Ensure the data consistency through row-level locking and MVCC. 3) Isolation: Supports multiple isolation levels, and REPEATABLEREAD is used by default. 4) Persistence: Use redolog to record modifications to ensure that data is saved for a long time.

MySQL's Place: Databases and ProgrammingMySQL's Place: Databases and ProgrammingApr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL: From Small Businesses to Large EnterprisesMySQL: From Small Businesses to Large EnterprisesApr 13, 2025 am 12:17 AM

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?What are phantom reads and how does InnoDB prevent them (Next-Key Locking)?Apr 13, 2025 am 12:16 AM

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft