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
What Are the Limitations of Using Views in MySQL?What Are the Limitations of Using Views in MySQL?May 14, 2025 am 12:10 AM

MySQLviewshavelimitations:1)Theydon'tsupportallSQLoperations,restrictingdatamanipulationthroughviewswithjoinsorsubqueries.2)Theycanimpactperformance,especiallywithcomplexqueriesorlargedatasets.3)Viewsdon'tstoredata,potentiallyleadingtooutdatedinforma

Securing Your MySQL Database: Adding Users and Granting PrivilegesSecuring Your MySQL Database: Adding Users and Granting PrivilegesMay 14, 2025 am 12:09 AM

ProperusermanagementinMySQLiscrucialforenhancingsecurityandensuringefficientdatabaseoperation.1)UseCREATEUSERtoaddusers,specifyingconnectionsourcewith@'localhost'or@'%'.2)GrantspecificprivilegeswithGRANT,usingleastprivilegeprincipletominimizerisks.3)

What Factors Influence the Number of Triggers I Can Use in MySQL?What Factors Influence the Number of Triggers I Can Use in MySQL?May 14, 2025 am 12:08 AM

MySQLdoesn'timposeahardlimitontriggers,butpracticalfactorsdeterminetheireffectiveuse:1)Serverconfigurationimpactstriggermanagement;2)Complextriggersincreasesystemload;3)Largertablesslowtriggerperformance;4)Highconcurrencycancausetriggercontention;5)M

MySQL: Is it safe to store BLOB?MySQL: Is it safe to store BLOB?May 14, 2025 am 12:07 AM

Yes,it'ssafetostoreBLOBdatainMySQL,butconsiderthesefactors:1)StorageSpace:BLOBscanconsumesignificantspace,potentiallyincreasingcostsandslowingperformance.2)Performance:LargerrowsizesduetoBLOBsmayslowdownqueries.3)BackupandRecovery:Theseprocessescanbe

MySQL: Adding a user through a PHP web interfaceMySQL: Adding a user through a PHP web interfaceMay 14, 2025 am 12:04 AM

Adding MySQL users through the PHP web interface can use MySQLi extensions. The steps are as follows: 1. Connect to the MySQL database and use the MySQLi extension. 2. Create a user, use the CREATEUSER statement, and use the PASSWORD() function to encrypt the password. 3. Prevent SQL injection and use the mysqli_real_escape_string() function to process user input. 4. Assign permissions to new users and use the GRANT statement.

MySQL: BLOB and other no-sql storage, what are the differences?MySQL: BLOB and other no-sql storage, what are the differences?May 13, 2025 am 12:14 AM

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

MySQL Add User: Syntax, Options, and Security Best PracticesMySQL Add User: Syntax, Options, and Security Best PracticesMay 13, 2025 am 12:12 AM

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

MySQL: How to avoid String Data Types common mistakes?MySQL: How to avoid String Data Types common mistakes?May 13, 2025 am 12:09 AM

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

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),