spring配置c3p0连接池、spring的声明式事务管理

spring配置c3p0连接池、spring的声明式事务管理

一、spring配置c3p0连接池:

1、导入maven依赖:

<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.2</version>
</dependency>

2、在spring配置文件中配置连接池:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

	<!-- spring配置c3p0连接池 start -->
	<!-- 1.配置连接池 -->
	<!-- 1.1创建连接池对象,dataSource的名字不能改变 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 1.2设置连接池的属性 -->
		<property name="driverClass" value="com.mysql/jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///test-ssm"></property>
		<property name="user" value="root"></property>
		<property name="password" value="admin"></property>
	</bean>
	<!-- 2.创建jdbcTemplate对象,并注入连接池对象 -->
	<bean id="jdbctemplate" class="org.springframework.jdbc.core.JdbcTemplate">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 3.创建userDao对象,并注入jdbctemplate对象 -->
	<bean id="userDao" class="com.zwp.dao.UserDao">
		<property name="jdbcTemplate" ref="jdbctemplate"></property>
	</bean>
	<!-- 4.创建userService对象,并注入userDao对象 -->
	<bean id="userService" class="com.zwp.service.UserService">
		<property name="userDao" ref="userDao"></property>
	</bean>
	<!-- spring配置c3p0连接池 end -->
</beans>

3、相关类的代码:

public class UserDao {
	
	private JdbcTemplate jdbcTemplate;
	public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
		this.jdbcTemplate = jdbcTemplate;
	}
	
	public void add(){
		System.out.println("UserDao调用JdbcTemplate..");
		String sql="insert into user values(?,?)";
		jdbcTemplate.update(sql,"小张","666");
	}
}
public class UserService {
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void add(){
		System.out.println("service调用dao...");
		userDao.add();
	}
}

4、测试类:

public class Test2 {
	@Test
	public void test6(){
		ApplicationContext context=
				new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		
		UserService userService = (UserService) context.getBean("userService");
		System.out.println("调用service...");
		userService.add();
	}
}

5、运行结果:

spring配置c3p0连接池、spring的声明式事务管理

二、spring的声明式事务管理:

spring的声明式事务管理有两种实现:

(1)基于xml配置文件的实现;

(2)基于注解方式的实现;

1、基于xml配置文件实现:

步骤:第一步:配置连接池;

第二步:配置事务管理器;

第三步:配置事务增强;

第四步:配置切面;

spring配置文件配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">


	<!-- 声明式事务管理-基于xml配置文件实现start -->
	<!-- 1.配置连接池 -->
	<!-- 1.1创建连接池对象 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 1.2设置连接池的属性 -->
		<property name="driverClass" value="com.mysql/jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///test-ssm"></property>
		<property name="user" value="root"></property>
		<property name="password" value="admin"></property>
	</bean>
	<!-- 5.配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- 6.配置事务增强 -->
	<tx:advice id="txadvice" transaction-manager="transactionManager">
		<!-- 6.1 做事务操作 -->
		<tx:attributes>
			<!-- 6.2 设置进行事务操作的方法匹配规则 -->
			<tx:method name="account" propagation="REQUIRED"/>
		</tx:attributes>
	</tx:advice>
	<!-- 7.配置切面 -->
	<aop:config>
		<!-- 7.1 配置切入点 -->
		<aop:pointcut expression="execution(* com.zwp.service.UserService.*(..))" id="pointcut1"/>
		<!-- 7.2 配置切面 -->
		<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut1"/>
	</aop:config>	
	<!-- 声明式事务管理-基于xml配置文件实现end -->
</beans>
public class UserService {
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void account(){
		//小明增加1000
		userDao.increase();
		
		//出现异常:
		int i=10/0;
		
		//小李减少1000
		userDao.decrease();
	}
}
public class Test2 {

	@Test
	public void test6(){
		ApplicationContext context=
				new ClassPathXmlApplicationContext("classpath:spring/applicationContext.xml");
		
		UserService userService = (UserService) context.getBean("userService");
		System.out.println("调用service...");
		userService.account();;
	}
}

运行结果:在执行account()方法是,出现异常,但是发生异常前的数据库操作没有被保存到数据库,说明配置成功了。

2、基于注解方式的实现:

步骤:第一步:配置连接池;

第二步:配置事务管理器;

第三步:开启事务注解;

第四步:在要使用事务的方法所在类上面添加注解@Transactional。

spring配置文件配置如下:

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
	http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
	http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
	http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">



	<!-- spring配置c3p0连接池 start -->
	<!-- 1.配置连接池 -->
	<!-- 1.1创建连接池对象 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 1.2设置连接池的属性 -->
		<property name="driverClass" value="com.mysql/jdbc.Driver"></property>
		<property name="jdbcUrl" value="jdbc:mysql:///test-ssm"></property>
		<property name="user" value="root"></property>
		<property name="password" value="admin"></property>
	</bean>
	<!-- spring配置c3p0连接池 end -->
	
	<!-- 5.配置事务管理器 -->
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	
	<!-- 开启事务注解 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

</beans>
//第四步:在要使用事务的方法所在类上面添加注解@Transactional
@Transactional
public class UserService {
	private UserDao userDao;
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void account(){
		//小明增加1000
		userDao.increase();
		
		//出现异常:
		int i=10/0;
		
		//小李减少1000
		userDao.decrease();
	}
}
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/114730.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 进销存软件开源java_JSH_ERP 开源版J2EE进销存系统代码源码 v1.0.2「建议收藏」

    进销存软件开源java_JSH_ERP 开源版J2EE进销存系统代码源码 v1.0.2「建议收藏」JSH_ERP是一个完整开源版的J2EE进销存系统代码。很多人说JSH_ERP是目前唯一完整开源的进销存系统虽然目前只有进销存+财务的功能,但后面将会推出ERP的全部功能,大家一起努力吧JSH_ERP立志为中小企业提供免费好用的ERP软件,降低企业的信息化成本个人开发者也可以使用JSH_EPP进行二次开发,承接外包ERP项目初学JAVA的小伙伴可以下载源代码来进行学习交流系统部署初始账号:jsh,…

    2022年5月6日
    54
  • Wi-Fi曝安全漏洞 面临KRACK攻击风险

    Wi-Fi曝安全漏洞 面临KRACK攻击风险近日,WPA2被曝存在严重安全漏洞。WPA2在2004年发布,自2006年3月起已经成为一种强制性的标准,是目前使用范围最广的Wi-Fi网络保护协议。何为KRACK攻击?在回答这个问题之前,让我们快速普及一些Crypto101课程的内容。高级加密标准(AES)已经采用了十几年。它是一种对称密钥密码,即使用相同的密钥来加密和解密。虽然传统上标准的AE…

    2022年5月5日
    38
  • docker-compose 集群_docker集群管理

    docker-compose 集群_docker集群管理前言实际工作中我们部署一个应用,一般不仅仅只有一个容器,可能会涉及到多个,比如用到数据库,中间件MQ,web前端和后端服务,等多个容器。我们如果一个个去启动应用,当项目非常多时,就很难记住了,所有

    2022年7月30日
    7
  • 分布式事务TCC方案Hmily——springcloud + feign + mybatis

    分布式事务TCC方案Hmily——springcloud + feign + mybatisTCC理论:分布式事务基础理论——TCCHmily介绍:分布式事务TCC方案——Hmily金融级柔性分布式事务解决方案介绍本文demo代码:GitHub依赖<dependency><groupId>org.dromara</groupId><artifactId>hmily-springcloud</artifactId><vers

    2022年5月13日
    69
  • windows server2016搭建ftp服务器_搭建网站的服务器

    windows server2016搭建ftp服务器_搭建网站的服务器WindowsServer2016系统中,快速搭建FTP服务器,作为文件服务器,下面分三步说明:安装IIS和FTP服务 配置FTP服务 测试FTP一、安装IIS和FTP服务首先点击开始菜单,进入找到服务器管理器,点击打开。进入后,找到管理菜单,点击添加角色和功能。进入服务器系统打开“服务器管理器”,点击“添加角色和功能”进入角色添加向导,一直点击下一步按钮到服务…

    2022年9月11日
    0
  • h5播放rtsp流_h5页面嵌入微信公众号

    h5播放rtsp流_h5页面嵌入微信公众号项目需求最近遇到一个新需求,将rtsp视频流接入h5页面中,rtsp是无法直接在h5页面上显示的,所以得通过一些手段将视频转成可以在h5上显示的格式;博主尝试过用nginx+ffmpeg转流,也尝试过用bilibili开源的flv.js转流;但最终效果都不太好,延迟高,卡顿时间长;后面发现一个神器VLC客户端,实时播放,完全不卡顿。前期准备VLC下载链接根据上方链接下载VLC客户端,根据自己的操作系统下载安装,博主使用的是windows10系统实际开发1.确保rtsp视频流可用,海康威视IP

    2022年8月31日
    0

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号