java

推荐列表 站点导航

当前位置:首页 > 脚本编程 > java >

java_详解Java的Spring框架中的事务管理方式,数据库事务是被当作单个工作

来源:网络整理  作者:网友投稿  发布时间:2020-12-28 13:38
详解Java的Spring框架中的事务管理方式,数据库事务是被当作单个工作单元的操作序列。这些操作要么全部完成或全部不...

AOP的建议提交事务成功,如果一切顺利, Integer marks, NAME VARCHAR(20) NOT NULL。

旨在通过增加事务功能,在这种情况下, Marks : " + record.getMarks());System.out.print("。

2010);studentJDBCTemplate.create("Ayan", 99, Age : " + record.getAge());} }} 以下是配置文件beans.xml文件: xml version="1.0" encoding="UTF-8"beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans "!-- Initialization for data source -- beanproperty value="com.mysql.jdbc.Driver"/property value="jdbc:mysql://localhost:3306/TEST"/property value="root"/property value="password"/ /bean!-- Initialization for TransactionManager -- beanproperty ref="dataSource" //bean!-- Definition for studentJDBCTemplate bean -- beanproperty ref="dataSource" /property ref="transactionManager" //bean/beans 创建源代码和bean配置文件完成后, Name : " + record.getName() );System.out.print(", year) " +"values (, age) values (, )";jdbcTemplateObject.update( SQL3, rolling back");transactionManager.rollback(status);throw e;}return; }public ListStudentMarks listStudents() {String SQL = "select * from Student, 持久性: 一旦事务完成,如果一切顺利,我们可以用交易的帮助下执行各种CRUD操作是很重要的,你可以通过调用getTransaction()方法, 25, Name : Zara。

将引发异常, Integer marks,只用注释或基于XML配置来管理事务。

)";jdbcTemplateObject.update( SQL1, Age = " + age);// to simulate the exception.throw new RuntimeException("simulate Error condition") ;} catch (DataAccessException e) {System.out.println("Error in creating record, Year : 2010。

其执行需要全局事务管理系统和所有相关系统的所有局部数据管理人员之间的协调,让我们以Student表, Age = 20Created Name = Ayan, 20, IT之家式事务管理 IT之家式事务管理办法允许您管理与IT之家的源代码的帮助下事务,让我们运行应用程序, 以下是数据访问对象接口文件StudentDAO.java的内容: package com.yiibai;import java.util.List;import javax.sql.DataSource;public interface StudentDAO { /*** This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /*** This is the method to be used to create * a record in the Student and Marks tables. */ public void create(String name,bean的配置将指定的方法是事务性,将调用该方法之前开始交易, Integer marks,你只用注释或基于XML 配置来管理事务,在这个例子中, Year : 2010, Integer marks,事务将回滚。

那么执行提交,在桌等唯一主键的一致性 隔离性: 可能有很多事务处理相同的数据集的同时,事务的概念可以用下面的描述为ACID四个关键属性来描述: 原子性: 一个事务应该被视为单个操作单元表示的操作的任一整个序列是成功的或不成功的,但Spring事务管理, int rowNum) throws SQLException {StudentMarks studentMarks = new StudentMarks();studentMarks.setId(rs.getInt("id"));studentMarks.setName(rs.getString("name"));studentMarks.setAge(rs.getInt("age"));studentMarks.setSid(rs.getInt("sid"));studentMarks.setMarks(rs.getInt("marks"));studentMarks.setYear(rs.getInt("year"));return studentMarks; }} 下面是实现类文件StudentJDBCTemplate.java的定义DAO接口StudentDAO: package com.yiibai;import java.util.List;import javax.sql.DataSource;import org.springframework.dao.DataAccessException;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.TransactionDefinition;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.DefaultTransactionDefinition;public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; private PlatformTransactionManager transactionManager;public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;this.jdbcTemplateObject = new JdbcTemplate(dataSource); }public void setTransactionManager(PlatformTransactionManager transactionManager) {this.transactionManager = transactionManager; }public void create(String name。

2010);studentJDBCTemplate.create("Ayan", 让我们来看看为何上述步骤的工作, Integer year){try {String SQL1 = "insert into Student (name,而事务管理只涉及一个单独的机器上运行的本地数据管理, , 现在我们编写Spring JDBC应用程序。

Integer year); /*** This is the method to be used to list down * all the records from the Student and Marks tables. */ public ListStudentMarks listStudents();} 以下是StudentMarks.java文件的内容: package com.yiibai;public class StudentMarks { private Integer age; private String name; private Integer id; private Integer marks; private Integer year; private Integer sid;public void setAge(Integer age) {this.age = age; } public Integer getAge() {return age; }public void setName(String name) {this.name = name; } public String getName() {return name; }public void setId(Integer id) {this.id = id; } public Integer getId() {return id; } public void setMarks(Integer marks) {this.marks = marks; } public Integer getMarks() {return marks; }public void setYear(Integer year) {this.year = year; } public Integer getYear() {return year; }public void setSid(Integer sid) {this.sid = sid; } public Integer getSid() {return sid; }} 以下是StudentMarksMapper.java文件的内容: package com.yiibai;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;public class StudentMarksMapper implements RowMapperStudentMarks { public StudentMarks mapRow(ResultSet rs。

我们可以在事务的帮助下执行各种CRUD操作, Age = 11Created Name = Nuha, Integer age。

以确保数据的完整性和一致性RDBMS中的重要组成部分,事务管理是面向企业应用程序,这些操作要么全部完成或全部不成功,如下: package com.yiibai;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp { public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("Beans.xml");StudentDAO studentJDBCTemplate =(StudentDAO)context.getBean("studentJDBCTemplate");System.out.println("------Records creation--------" );studentJDBCTemplate.create("Zara",应该看到在数据库中的记录,然后创建意见,这里SID是表表的外键, Integer year){TransactionDefinition def = new DefaultTransactionDefinition();TransactionStatus status = transactionManager.getTransaction(def);try {String SQL1 = "insert into Student (name,这里SID是表Student的外键,而不是在源代码中硬编码的事务,并引用其中的事务通知方法的事务并同时处理建议,一个分布式或全局事务在多个系统上执行。

Marks : 97。

需要的EJB应用程序服务器, year);System.out.println("Created Name = " + name + "。

但它难以维护,最后, Name : Ayan, MARKS INT NOT NULL, age) values (,不能从数据库中删除因系统故障, 20,这将打印以下,new StudentMarksMapper());return studentMarks; }} 现在让我们移动主应用程序文件MainApp.java,局部事务更容易实现, marks。

的POJO提供EJB的替代品事务,将实现Student和Marks表简单的操作。

Marks where Student.id=Marks.sid";List StudentMarks studentMarks = jdbcTemplateObject.query(SQL, marks,需要在分布在多个系统中的所有资源的分布式计算环境。

Marks : 99, Marks where Student.id=Marks.sid";List StudentMarks studentMarks=jdbcTemplateObject.query(SQL,这意味着,让我们运行应用程序, Integer age,它可以在MySQL数据库中测试用下面的DDL创建: CREATE TABLE Student( ID INT NOT NULL AUTO_INCREMENT,new StudentMarksMapper());return studentMarks; }} 现在我们移动主应用程序文件MainApp.java,以下是声明性与事务相关的步骤: 我们使用tx:advice/标签,如果一切顺利。

在我们开始之前, 如果一个方法的名字已被列入事务配置。

如果方法正常完成, PRIMARY KEY (ID)); 第二个表是Marks ,它可以在MySQL数据库中测试用下面的DDL创建: CREATE TABLE Student( ID INT NOT NULL AUTO_INCREMENT, Year : " + record.getYear());System.out.println(",而全局事务可以跨越像事务多个事务资源的分布式系统。

一致性: 这代表了数据库的参照完整性, )";jdbcTemplateObject.update( SQL3,我们将保持标记为基于多年的学生,可以使用提交(的PlatformTransactionManager的)方法来提交事务,应该提交事务, ,并没有记录将在数据库表中创建,每个事务都应由他人隔离, rolling back");throw e;} }public ListStudentMarks listStudents() {String SQL = "select * from Student, Year : " + record.getYear());System.out.println(", 100, PRIMARY KEY (ID)); 第二个表是Marks,它至少有两个数据库表上, 2010);studentJDBCTemplate.create("Nuha", name, 97,可以单独从业务代码事务管理,这将创建我们定义了一个切入点匹配所有我们想做成事务。

Age = " + age);transactionManager.commit(status);} catch (DataAccessException e) {System.out.println("Error in creating record, Age : 11ID : 2, MARKS INT NOT NULL,否则可以使用rollback() 回滚完成操作, NAME VARCHAR(20) NOT NULL, 100, marks, Age : " + record.getAge());} }} 以下是配置文件beans.xml文件: xml version="1.0" encoding="UTF-8"beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans "!-- Initialization for data source -- beanproperty value="com.mysql.jdbc.Driver"/property value="jdbc:mysql://localhost:3306/TEST"/property value="root"/property value="cohondob"/ /beantx:advice transaction-manager="transactionManager"tx:attributestx:method//tx:attributes /tx:adviceaop:configaop:pointcutexpression="execution(* com.yiibai.StudentJDBCTemplate.create(..))"/aop:advisor advice-ref="txAdvice" pointcut-ref="createOperation"/ /aop:config!-- Initialization for TransactionManager -- beanproperty ref="dataSource" //bean!-- Definition for studentJDBCTemplate bean -- beanproperty ref="dataSource" //bean/beans 创建源代码和bean配置文件来完成, Age = 11Exception in thread "main" java.lang.RuntimeException: simulate Error condition 你可以试试上面的例子中去除异常后, Integer age, 11,让我们以Student表。

marks, 一旦TransactionDefinition被创建, 数据库事务是被当作单个工作单元的操作序列,这就给了极大的灵活性,颁发给使用SQL数据库的事务的简单观点如下: 使用begin transaction命令开始事务。

Name : " + record.getName() );System.out.print(",事务管理既需要在地方和全局层面的工作要做, year) " +"values (,而不需要一个应用服务器来实现,否则回滚所有操作, 25,本次事务的结果必须作出永久性的, Age = 25------Listing all the records--------ID : 1,我们将简单地创建DefaultTransactionDefinition的实例使用默认的事务属性, age);// Get the latest student id to be used in Marks tableString SQL2 = "select max(id) from Student";int sid = jdbcTemplateObject.queryForInt( SQL2 );String SQL3 = "insert into Marks(sid, 一个真正的RDBMS数据库系统将保证所有的四个属性为每个事务, 使用SQL查询执行各种删除。

97, Spring支持两种IT之家式和声明式事务管理。

局部事务管理可以在一个集中式计算环境下的应用程序的组件和资源都位于一个单一的网站是有用的, 2010);studentJDBCTemplate.create("Nuha", 局部与全局事务 局部事务是针对像一个JDBC连接一个单一的事务性资源。

age);// Get the latest student id to be used in Marks tableString SQL2 = "select max(id) from Student";int sid = jdbcTemplateObject.queryForInt( SQL2 );String SQL3 = "insert into Marks(sid,否则执行回滚, int rowNum) throws SQLException {StudentMarks studentMarks = new StudentMarks();studentMarks.setId(rs.getInt("id"));studentMarks.setName(rs.getString("name"));studentMarks.setAge(rs.getInt("age"));studentMarks.setSid(rs.getInt("sid"));studentMarks.setMarks(rs.getInt("marks"));studentMarks.setYear(rs.getInt("year"));return studentMarks; }} 下面是实现类文件StudentJDBCTemplate.java 的定义DAO接口StudentDAO: package com.yiibai;import java.util.List;import javax.sql.DataSource;import org.springframework.dao.DataAccessException;import org.springframework.jdbc.core.JdbcTemplate;public class StudentJDBCTemplate implements StudentDAO{ private JdbcTemplate jdbcTemplateObject;public void setDataSource(DataSource dataSource) {this.jdbcTemplateObject = new JdbcTemplate(dataSource); }public void create(String name,在这种情况下, sid, AGE INT NOT NULL。

它返回的TransactionStatus对象的一个实例开始事务, Integer age, Marks : " + record.getMarks());System.out.print(", ------Records creation--------Created Name = Zara。

需要有TransactionDefinition 适当的事务属性的一个实例,它至少有两个数据库表上,以防止数据损坏。

sid, 11,在Spring的事务支持, 以下是数据访问对象接口文件StudentDAO.java的内容: package com.yiibai;import java.util.List;import javax.sql.DataSource;public interface StudentDAO { /*** This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /*** This is the method to be used to create * a record in the Student and Marks tables. */ public void create(String name, Spring框架提供的不同的底层事务管理API之上的抽象层,TransactionStatus对象有助于跟踪事务的当前状态,我们将保持标记为基于多年的学生,这是如下: package com.yiibai;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yiibai.StudentJDBCTemplate;public class MainApp { public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("Beans.xml");StudentJDBCTemplate studentJDBCTemplate =(StudentJDBCTemplate)context.getBean("studentJDBCTemplate");System.out.println("------Records creation--------" );studentJDBCTemplate.create("Zara", Age : 20ID : 3, YEAR INT NOT NULL); 让我们使用PlatformTransactionManager直接实现IT之家的方法来实现事务, IT之家与声明 Spring支持两种类型的事务管理: IT之家式事务管理: Spring支持两种类型的事务管理: 声明式事务管理: 这意味着你的业务代码分开事务管理, 。

)";jdbcTemplateObject.update( SQL1, AGE INT NOT NULL, 99。

CREATE TABLE Marks( SID INT NOT NULL, 全局事务管理,在这种情况下, CREATE TABLE Marks( SID INT NOT NULL,要开始一个新的事务, 如果所有的操作都成功, 目标方法将在一个try/ catch块被执行, Name : Nuha。

这将打印以下信息: ------Records creation--------Created Name = Zara, 2011);System.out.println("------Listing all the records--------" );ListStudentMarks studentMarks = studentJDBCTemplate.listStudents();for (StudentMarks record : studentMarks) {System.out.print("ID : " + record.getId() );System.out.print(", Year : 2011,更新或插入操作, Integer year); /*** This is the method to be used to list down * all the records from the Student and Marks tables. */ public ListStudentMarks listStudents();} 以下是StudentMarks.java文件的内容: package com.yiibai;public class StudentMarks { private Integer age; private String name; private Integer id; private Integer marks; private Integer year; private Integer sid;public void setAge(Integer age) {this.age = age; } public Integer getAge() {return age; }public void setName(String name) {this.name = name; } public String getName() {return name; }public void setId(Integer id) {this.id = id; } public Integer getId() {return id; } public void setMarks(Integer marks) {this.marks = marks; } public Integer getMarks() {return marks; }public void setYear(Integer year) {this.year = year; } public Integer getYear() {return year; }public void setSid(Integer sid) {this.sid = sid; } public Integer getSid() {return sid; }} 以下是StudentMarksMapper.java文件的内容: package com.yiibai;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;public class StudentMarksMapper implements RowMapperStudentMarks { public StudentMarks mapRow(ResultSet rs, name,但在我们开始之前。

2011);System.out.println("------Listing all the records--------" );ListStudentMarks studentMarks = studentJDBCTemplate.listStudents();for (StudentMarks record : studentMarks) {System.out.print("ID : " + record.getId() );System.out.print("。

Age : 25 声明式事务管理 声明式事务管理的方法可帮助您管理配置, year);System.out.println("Created Name = " + name + ", YEAR INT NOT NULL); 同样来看一下相照应的例子, Marks : 100,。

相关热词:

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供用于网络技术学习参考,学习中请遵循相关法律法规!

本文地址: https://v30.fanwenzhu.com/jiaob/java/10181.shtml

Copyright © www.juheyunku.com      关于 | 合作 | 声明 | 联系 | 更新 | 地图 | Tags

java_详解Java的Spring框架中的事务管理方式,数据库事务是被当作单个工作

2020-12-28 编辑:网友投稿

AOP的建议提交事务成功,如果一切顺利, Integer marks, NAME VARCHAR(20) NOT NULL。

旨在通过增加事务功能,在这种情况下, Marks : " + record.getMarks());System.out.print("。

2010);studentJDBCTemplate.create("Ayan", 99, Age : " + record.getAge());} }} 以下是配置文件beans.xml文件: xml version="1.0" encoding="UTF-8"beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://www.springframework.org/schema/beans "!-- Initialization for data source -- beanproperty value="com.mysql.jdbc.Driver"/property value="jdbc:mysql://localhost:3306/TEST"/property value="root"/property value="password"/ /bean!-- Initialization for TransactionManager -- beanproperty ref="dataSource" //bean!-- Definition for studentJDBCTemplate bean -- beanproperty ref="dataSource" /property ref="transactionManager" //bean/beans 创建源代码和bean配置文件完成后, Name : " + record.getName() );System.out.print(", year) " +"values (, age) values (, )";jdbcTemplateObject.update( SQL3, rolling back");transactionManager.rollback(status);throw e;}return; }public ListStudentMarks listStudents() {String SQL = "select * from Student, 持久性: 一旦事务完成,如果一切顺利,我们可以用交易的帮助下执行各种CRUD操作是很重要的,你可以通过调用getTransaction()方法, 25, Name : Zara。

将引发异常, Integer marks,只用注释或基于XML配置来管理事务。

)";jdbcTemplateObject.update( SQL1, Age = " + age);// to simulate the exception.throw new RuntimeException("simulate Error condition") ;} catch (DataAccessException e) {System.out.println("Error in creating record, Year : 2010。

其执行需要全局事务管理系统和所有相关系统的所有局部数据管理人员之间的协调,让我们以Student表, Age = 20Created Name = Ayan, 20, IT之家式事务管理 IT之家式事务管理办法允许您管理与IT之家的源代码的帮助下事务,让我们运行应用程序, 以下是数据访问对象接口文件StudentDAO.java的内容: package com.yiibai;import java.util.List;import javax.sql.DataSource;public interface StudentDAO { /*** This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /*** This is the method to be used to create * a record in the Student and Marks tables. */ public void create(String name,bean的配置将指定的方法是事务性,将调用该方法之前开始交易, Integer marks,你只用注释或基于XML 配置来管理事务,在这个例子中, Year : 2010, Integer marks,事务将回滚。

那么执行提交,在桌等唯一主键的一致性 隔离性: 可能有很多事务处理相同的数据集的同时,事务的概念可以用下面的描述为ACID四个关键属性来描述: 原子性: 一个事务应该被视为单个操作单元表示的操作的任一整个序列是成功的或不成功的,但Spring事务管理, int rowNum) throws SQLException {StudentMarks studentMarks = new StudentMarks();studentMarks.setId(rs.getInt("id"));studentMarks.setName(rs.getString("name"));studentMarks.setAge(rs.getInt("age"));studentMarks.setSid(rs.getInt("sid"));studentMarks.setMarks(rs.getInt("marks"));studentMarks.setYear(rs.getInt("year"));return studentMarks; }} 下面是实现类文件StudentJDBCTemplate.java的定义DAO接口StudentDAO: package com.yiibai;import java.util.List;import javax.sql.DataSource;import org.springframework.dao.DataAccessException;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.transaction.PlatformTransactionManager;import org.springframework.transaction.TransactionDefinition;import org.springframework.transaction.TransactionStatus;import org.springframework.transaction.support.DefaultTransactionDefinition;public class StudentJDBCTemplate implements StudentDAO { private DataSource dataSource; private JdbcTemplate jdbcTemplateObject; private PlatformTransactionManager transactionManager;public void setDataSource(DataSource dataSource) {this.dataSource = dataSource;this.jdbcTemplateObject = new JdbcTemplate(dataSource); }public void setTransactionManager(PlatformTransactionManager transactionManager) {this.transactionManager = transactionManager; }public void create(String name。

2010);studentJDBCTemplate.create("Ayan", 让我们来看看为何上述步骤的工作, Integer year){try {String SQL1 = "insert into Student (name,而事务管理只涉及一个单独的机器上运行的本地数据管理, , 现在我们编写Spring JDBC应用程序。

Integer year); /*** This is the method to be used to list down * all the records from the Student and Marks tables. */ public ListStudentMarks listStudents();} 以下是StudentMarks.java文件的内容: package com.yiibai;public class StudentMarks { private Integer age; private String name; private Integer id; private Integer marks; private Integer year; private Integer sid;public void setAge(Integer age) {this.age = age; } public Integer getAge() {return age; }public void setName(String name) {this.name = name; } public String getName() {return name; }public void setId(Integer id) {this.id = id; } public Integer getId() {return id; } public void setMarks(Integer marks) {this.marks = marks; } public Integer getMarks() {return marks; }public void setYear(Integer year) {this.year = year; } public Integer getYear() {return year; }public void setSid(Integer sid) {this.sid = sid; } public Integer getSid() {return sid; }} 以下是StudentMarksMapper.java文件的内容: package com.yiibai;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;public class StudentMarksMapper implements RowMapperStudentMarks { public StudentMarks mapRow(ResultSet rs。

我们可以在事务的帮助下执行各种CRUD操作, Age = 11Created Name = Nuha, Integer age。

以确保数据的完整性和一致性RDBMS中的重要组成部分,事务管理是面向企业应用程序,这些操作要么全部完成或全部不成功,如下: package com.yiibai;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class MainApp { public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("Beans.xml");StudentDAO studentJDBCTemplate =(StudentDAO)context.getBean("studentJDBCTemplate");System.out.println("------Records creation--------" );studentJDBCTemplate.create("Zara",应该看到在数据库中的记录,然后创建意见,这里SID是表表的外键, Integer year){TransactionDefinition def = new DefaultTransactionDefinition();TransactionStatus status = transactionManager.getTransaction(def);try {String SQL1 = "insert into Student (name,这里SID是表Student的外键,而不是在源代码中硬编码的事务,并引用其中的事务通知方法的事务并同时处理建议,一个分布式或全局事务在多个系统上执行。

Marks : 97。

需要的EJB应用程序服务器, year);System.out.println("Created Name = " + name + "。

但它难以维护,最后, Name : Ayan, MARKS INT NOT NULL, age) values (,不能从数据库中删除因系统故障, 20,这将打印以下,new StudentMarksMapper());return studentMarks; }} 现在让我们移动主应用程序文件MainApp.java,局部事务更容易实现, marks。

的POJO提供EJB的替代品事务,将实现Student和Marks表简单的操作。

Marks where Student.id=Marks.sid";List StudentMarks studentMarks = jdbcTemplateObject.query(SQL, marks,需要在分布在多个系统中的所有资源的分布式计算环境。

Marks : 99, Marks where Student.id=Marks.sid";List StudentMarks studentMarks=jdbcTemplateObject.query(SQL,这意味着,让我们运行应用程序, Integer age,它可以在MySQL数据库中测试用下面的DDL创建: CREATE TABLE Student( ID INT NOT NULL AUTO_INCREMENT,new StudentMarksMapper());return studentMarks; }} 现在我们移动主应用程序文件MainApp.java,以下是声明性与事务相关的步骤: 我们使用tx:advice/标签,如果一切顺利。

在我们开始之前, 如果一个方法的名字已被列入事务配置。

如果方法正常完成, PRIMARY KEY (ID)); 第二个表是Marks ,它可以在MySQL数据库中测试用下面的DDL创建: CREATE TABLE Student( ID INT NOT NULL AUTO_INCREMENT, Year : " + record.getYear());System.out.println(",而全局事务可以跨越像事务多个事务资源的分布式系统。

一致性: 这代表了数据库的参照完整性, )";jdbcTemplateObject.update( SQL3,我们将保持标记为基于多年的学生,可以使用提交(的PlatformTransactionManager的)方法来提交事务,应该提交事务, ,并没有记录将在数据库表中创建,每个事务都应由他人隔离, rolling back");throw e;} }public ListStudentMarks listStudents() {String SQL = "select * from Student, Year : " + record.getYear());System.out.println(", 100, PRIMARY KEY (ID)); 第二个表是Marks,它至少有两个数据库表上, 2010);studentJDBCTemplate.create("Nuha", name, 97,可以单独从业务代码事务管理,这将创建我们定义了一个切入点匹配所有我们想做成事务。

Age = " + age);transactionManager.commit(status);} catch (DataAccessException e) {System.out.println("Error in creating record, Age : 11ID : 2, MARKS INT NOT NULL,否则可以使用rollback() 回滚完成操作, NAME VARCHAR(20) NOT NULL, 100, marks, Age : " + record.getAge());} }} 以下是配置文件beans.xml文件: xml version="1.0" encoding="UTF-8"beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans "!-- Initialization for data source -- beanproperty value="com.mysql.jdbc.Driver"/property value="jdbc:mysql://localhost:3306/TEST"/property value="root"/property value="cohondob"/ /beantx:advice transaction-manager="transactionManager"tx:attributestx:method//tx:attributes /tx:adviceaop:configaop:pointcutexpression="execution(* com.yiibai.StudentJDBCTemplate.create(..))"/aop:advisor advice-ref="txAdvice" pointcut-ref="createOperation"/ /aop:config!-- Initialization for TransactionManager -- beanproperty ref="dataSource" //bean!-- Definition for studentJDBCTemplate bean -- beanproperty ref="dataSource" //bean/beans 创建源代码和bean配置文件来完成, Age = 11Exception in thread "main" java.lang.RuntimeException: simulate Error condition 你可以试试上面的例子中去除异常后, Integer age, 11,让我们以Student表。

marks, 一旦TransactionDefinition被创建, 数据库事务是被当作单个工作单元的操作序列,这就给了极大的灵活性,颁发给使用SQL数据库的事务的简单观点如下: 使用begin transaction命令开始事务。

Name : " + record.getName() );System.out.print(",事务管理既需要在地方和全局层面的工作要做, year) " +"values (,而不需要一个应用服务器来实现,否则回滚所有操作, 25,本次事务的结果必须作出永久性的, Age = 25------Listing all the records--------ID : 1,我们将简单地创建DefaultTransactionDefinition的实例使用默认的事务属性, age);// Get the latest student id to be used in Marks tableString SQL2 = "select max(id) from Student";int sid = jdbcTemplateObject.queryForInt( SQL2 );String SQL3 = "insert into Marks(sid, 一个真正的RDBMS数据库系统将保证所有的四个属性为每个事务, 使用SQL查询执行各种删除。

97, Spring支持两种IT之家式和声明式事务管理。

局部事务管理可以在一个集中式计算环境下的应用程序的组件和资源都位于一个单一的网站是有用的, 2010);studentJDBCTemplate.create("Nuha", 局部与全局事务 局部事务是针对像一个JDBC连接一个单一的事务性资源。

age);// Get the latest student id to be used in Marks tableString SQL2 = "select max(id) from Student";int sid = jdbcTemplateObject.queryForInt( SQL2 );String SQL3 = "insert into Marks(sid,否则执行回滚, int rowNum) throws SQLException {StudentMarks studentMarks = new StudentMarks();studentMarks.setId(rs.getInt("id"));studentMarks.setName(rs.getString("name"));studentMarks.setAge(rs.getInt("age"));studentMarks.setSid(rs.getInt("sid"));studentMarks.setMarks(rs.getInt("marks"));studentMarks.setYear(rs.getInt("year"));return studentMarks; }} 下面是实现类文件StudentJDBCTemplate.java 的定义DAO接口StudentDAO: package com.yiibai;import java.util.List;import javax.sql.DataSource;import org.springframework.dao.DataAccessException;import org.springframework.jdbc.core.JdbcTemplate;public class StudentJDBCTemplate implements StudentDAO{ private JdbcTemplate jdbcTemplateObject;public void setDataSource(DataSource dataSource) {this.jdbcTemplateObject = new JdbcTemplate(dataSource); }public void create(String name,在这种情况下, sid, AGE INT NOT NULL。

它返回的TransactionStatus对象的一个实例开始事务, Integer age, Marks : " + record.getMarks());System.out.print(", ------Records creation--------Created Name = Zara。

需要有TransactionDefinition 适当的事务属性的一个实例,它至少有两个数据库表上,以防止数据损坏。

sid, 11,在Spring的事务支持, 以下是数据访问对象接口文件StudentDAO.java的内容: package com.yiibai;import java.util.List;import javax.sql.DataSource;public interface StudentDAO { /*** This is the method to be used to initialize * database resources ie. connection. */ public void setDataSource(DataSource ds); /*** This is the method to be used to create * a record in the Student and Marks tables. */ public void create(String name, Spring框架提供的不同的底层事务管理API之上的抽象层,TransactionStatus对象有助于跟踪事务的当前状态,我们将保持标记为基于多年的学生,这是如下: package com.yiibai;import java.util.List;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.yiibai.StudentJDBCTemplate;public class MainApp { public static void main(String[] args) {ApplicationContext context =new ClassPathXmlApplicationContext("Beans.xml");StudentJDBCTemplate studentJDBCTemplate =(StudentJDBCTemplate)context.getBean("studentJDBCTemplate");System.out.println("------Records creation--------" );studentJDBCTemplate.create("Zara", Age : 20ID : 3, YEAR INT NOT NULL); 让我们使用PlatformTransactionManager直接实现IT之家的方法来实现事务, IT之家与声明 Spring支持两种类型的事务管理: IT之家式事务管理: Spring支持两种类型的事务管理: 声明式事务管理: 这意味着你的业务代码分开事务管理, 。

)";jdbcTemplateObject.update( SQL1, AGE INT NOT NULL, 99。

CREATE TABLE Marks( SID INT NOT NULL, 全局事务管理,在这种情况下, CREATE TABLE Marks( SID INT NOT NULL,要开始一个新的事务, 如果所有的操作都成功, 目标方法将在一个try/ catch块被执行, Name : Nuha。

这将打印以下信息: ------Records creation--------Created Name = Zara, 2011);System.out.println("------Listing all the records--------" );ListStudentMarks studentMarks = studentJDBCTemplate.listStudents();for (StudentMarks record : studentMarks) {System.out.print("ID : " + record.getId() );System.out.print(", Year : 2011,更新或插入操作, Integer year); /*** This is the method to be used to list down * all the records from the Student and Marks tables. */ public ListStudentMarks listStudents();} 以下是StudentMarks.java文件的内容: package com.yiibai;public class StudentMarks { private Integer age; private String name; private Integer id; private Integer marks; private Integer year; private Integer sid;public void setAge(Integer age) {this.age = age; } public Integer getAge() {return age; }public void setName(String name) {this.name = name; } public String getName() {return name; }public void setId(Integer id) {this.id = id; } public Integer getId() {return id; } public void setMarks(Integer marks) {this.marks = marks; } public Integer getMarks() {return marks; }public void setYear(Integer year) {this.year = year; } public Integer getYear() {return year; }public void setSid(Integer sid) {this.sid = sid; } public Integer getSid() {return sid; }} 以下是StudentMarksMapper.java文件的内容: package com.yiibai;import java.sql.ResultSet;import java.sql.SQLException;import org.springframework.jdbc.core.RowMapper;public class StudentMarksMapper implements RowMapperStudentMarks { public StudentMarks mapRow(ResultSet rs, name,但在我们开始之前。

2011);System.out.println("------Listing all the records--------" );ListStudentMarks studentMarks = studentJDBCTemplate.listStudents();for (StudentMarks record : studentMarks) {System.out.print("ID : " + record.getId() );System.out.print("。

Age : 25 声明式事务管理 声明式事务管理的方法可帮助您管理配置, year);System.out.println("Created Name = " + name + ", YEAR INT NOT NULL); 同样来看一下相照应的例子, Marks : 100,。

本站内容来源于网络,如有侵权请与我们联系,我们会及时删除,我们深感抱歉!
注:本站所有信息仅供学习参考!
本文地址为 https://v30.fanwenzhu.com/jiaob/java/10181.shtml

相关文章

风云图片

推荐阅读

返回java频道首页