Spring之事务管理TranscationManager(大合集),nginx面试题2024

一个事务一旦提交,它对数据库中数据的改变就应该是永久性的。

在正常的web项目中,事务和DAO一直如影随行,所以有人认为配置事务和DAO的关系是密不可分,不能分离的。其实不然,DAO可以完全脱离容器独立存在。

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns=“http://www.springframework.org/schema/beans”

xmlns:p=“http://www.springframework.org/schema/p” xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”

xmlns:context=“http://www.springframework.org/schema/context” xmlns:aop=“http://www.springframework.org/schema/aop”

xmlns:mvc=“http://www.springframework.org/schema/mvc”

xmlns:tx=“http://www.springframework.org/schema/tx”

xmlns:ehcache=“http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring”

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-3.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-3.2.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx-3.2.xsd

http://www.springframework.org/schema/aop

http://www.springframework.org/schema/aop/spring-aop-3.2.xsd

http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring

http://ehcache-spring-annotations.googlecode.com/svn/schema/ehcache-spring/ehcache-spring-1.1.xsd">

<mvc:annotation-driven />

<context:component-scan base-package=“com.zhu” />

<bean id=“dataSource” class=“com.alibaba.druid.pool.DruidDataSource”

destroy-method=“close”

p:driverClassName=“com.mysql.jdbc.Driver”

p:username=“zhu”

p:password=“zhu” />

<bean id=“jdbcTemplate” class=“org.springframework.jdbc.core.JdbcTemplate”

p:dataSource-ref=“dataSource” />

zhu_test.sql

CREATE TABLE IF NOT EXISTS zhu_test (NAME VARCHAR(20),age INT(100)) ENGINE = INNODB;

测试类:

package com.zhu.service;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.DruidDataSource;

@Service(“service1”)

public class UserJdbcWithoutTransManagerService {

@Autowired

private JdbcTemplate jdbcTemplate;

public void addScore(String userName,int toAdd){

String sql = “UPDATE zhu_test u SET u.age = u.age + ? WHERE name =?”;

jdbcTemplate.update(sql,toAdd,userName);

}

public static void main(String[] args) {

ApplicationContext ctx =

new ClassPathXmlApplicationContext(“file:src/main/resources/applicationContext.xml”);

UserJdbcWithoutTransManagerService service =

(UserJdbcWithoutTransManagerService)ctx.getBean(“service1”);

JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean(“jdbcTemplate”);

DruidDataSource druidDataSource = (DruidDataSource)jdbcTemplate.getDataSource();

//①.检查数据源autoCommit的设置

System.out.println(“autoCommit:”+ druidDataSource.isDefaultAutoCommit());

//②.插入一条记录,初始分数为10

jdbcTemplate.execute(

“INSERT INTO zhu_test VALUES(‘tom’,10)”);

//③.调用工作在无事务环境下的服务类方法,将分数添加20分

service.addScore(“tom”,20);

//④.查看此时用户的分数

@SuppressWarnings(“deprecation”)

int score = jdbcTemplate.queryForInt(“SELECT age FROM zhu_test WHERE name =‘tom’”);

System.out.println(“score:”+score);

jdbcTemplate.execute(“DELETE FROM zhu_test WHERE name=‘tom’”);

}

}

运行结果:

这里写图片描述

applicationContext.xml中并没有配置事务,但是还是持久化到数据库中去了。DataSource默认设置是自动提交的,也就是说,在执行了CRUD之后,会马上持久化到数据库中。如果设置自动提交为false,那么在jdbcTemplate执行完之后并不会马上持久化到数据库中,除非手动提交。

虽说没有事务管理,程序依然可以进行数据的CRUD操作,但是没有事务管理,在数据安全同步方面会面临很大的挑战。栗子太多,不一一举例,看代码

我们故意提交一条错误的sql语句

package com.zhu.service;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.ApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import org.springframework.jdbc.core.JdbcTemplate;

import org.springframework.stereotype.Service;

import com.alibaba.druid.pool.DruidDataSource;

@Service(“service1”)

public class UserJdbcWithoutTransManagerService {

@Autowired

private JdbcTemplate jdbcTemplate;

public void addScore(String userName,int toAdd){

String sql = “UPDATE zhu_test u SET u.age = u.age + ? WHERE name =?”;

jdbcTemplate.update(sql,toAdd,userName);

}

public static void main(String[] args) {

ApplicationContext ctx =

new ClassPathXmlApplicationContext(“file:src/main/resources/applicationContext.xml”);

UserJdbcWithoutTransManagerService service =

(UserJdbcWithoutTransManagerService)ctx.getBean(“service1”);

JdbcTemplate jdbcTemplate = (JdbcTemplate)ctx.getBean(“jdbcTemplate”);

DruidDataSource druidDataSource = (DruidDataSource)jdbcTemplate.getDataSource();

//①.检查数据源autoCommit的设置

System.err.println(“autoCommit:”+ druidDataSource.isDefaultAutoCommit());

//②.插入一条记录,初始分数为10

jdbcTemplate.execute(

“INSERT INTO zhu_test VALUES(‘tom’,10)”);

//③.执行一条错误sql语句

jdbcTemplate.execute(

“INSERT INTO zhu_test VALUES(‘tom1’,10,00)”);

//④.调用工作在无事务环境下的服务类方法,将分数添加20分(不会执行)

service.addScore(“tom”,20);

}

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img
img
img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024b (备注Java)
img

我的面试宝典:一线互联网大厂Java核心面试题库

以下是我个人的一些做法,希望可以给各位提供一些帮助:

整理了很长一段时间,拿来复习面试刷题非常合适,其中包括了Java基础、异常、集合、并发编程、JVM、Spring全家桶、MyBatis、Redis、数据库、中间件MQ、Dubbo、Linux、Tomcat、ZooKeeper、Netty等等,且还会持续的更新…可star一下!

image

283页的Java进阶核心pdf文档

Java部分:Java基础,集合,并发,多线程,JVM,设计模式

数据结构算法:Java算法,数据结构

开源框架部分:Spring,MyBatis,MVC,netty,tomcat

分布式部分:架构设计,Redis缓存,Zookeeper,kafka,RabbitMQ,负载均衡等

微服务部分:SpringBoot,SpringCloud,Dubbo,Docker

image

还有源码相关的阅读学习

image

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
img

Redis缓存,Zookeeper,kafka,RabbitMQ,负载均衡等

微服务部分:SpringBoot,SpringCloud,Dubbo,Docker

[外链图片转存中…(img-n3LzlDiW-1712674692176)]

还有源码相关的阅读学习

[外链图片转存中…(img-vtWTJIKw-1712674692177)]

一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-RYjw3eRp-1712674692177)]

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值