Cloud TT

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
统计
 

摘要: 之前使用Spring的BeanNameUrlHandlerMapping,对Hessian的处理是比较简单的,这里不多说了。那么如果提供ResuFul 通过Spring的 Annotation功能呢?据我所知,Spring支持的Hessian目前还不支持Annotation配置。好吧,想想有没有别的办法:因为 Annotation 的 restful 需要启动这两个类:[代码]这样的话,Sprin... 阅读全文
posted @ 2010-07-31 10:27 AnsonYang 阅读(3730) 评论(6) 推荐(0) 编辑
 

2016年3月10日

背景:

项目中遇到有一系列对Redis的操作,并需要保持事务处理。

环境:

Spring version 4.1.8.RELEASE

Redis Server 2.6.12 (64位)

spring-data-redis version 1.6.1.RELEASE

jedis version 2.7.3

 

使用Spring的@Transactional对redis操作进行控制

Spring Redis的配置文件如下:

<description>Jedis配置</description>

    <!-- Redis 配置 -->
    <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <property name="maxIdle" value="${redis.maxIdle}" />
        <property name="maxTotal" value="${redis.maxActive}" />
        <property name="maxWaitMillis" value="${redis.maxWait}" />
        <property name="testOnBorrow" value="${redis.testOnBorrow}" />
    </bean>

    <bean id="jedisConnectionFactory"
        class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
        p:hostName="${redis.host}" p:port="${redis.port}" p:usePool="true"
        p:timeout="${redis.timeout}" p:password="${redis.pass}" p:database="${redis.default.db}"
        p:poolConfig-ref="jedisPoolConfig" />

    <bean id="stringRedisSerializer"
        class="org.springframework.data.redis.serializer.StringRedisSerializer" />

    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="jedisConnectionFactory"
        p:defaultSerializer-ref="stringRedisSerializer"
        p:enableTransactionSupport="false" />

    <bean id="redisTemplateTransactional" class="org.springframework.data.redis.core.RedisTemplate"
        p:connection-factory-ref="jedisConnectionFactory"
        p:defaultSerializer-ref="stringRedisSerializer"
        p:enableTransactionSupport="true" />

其中的p:enableTransactionSupport 需要设置为true,才能开启Redis事务管理控制。

业务代码如下:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.RedisSystemException;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Autowired
private RedisTemplate<String, String> redisTemplateTransactional; @Transactional public void transactionalForRedis() throws Exception {   redisTemplateTransactional.boundValueOps("name").set("TEST_XXX");
  redisTemplateTransactional.boundValueOps(
"age").set("11");
  // 异常代码   
for (int i = 0; i < 5; i++) {     if (i == 3) {
      throw new RedisSystemException("dsd", new Exception("myself exception....."));     }   } }

1.使用Spring的事务控制即可:@Transactional

2.其中会抛出异常处理,必须抛出RedisSystemException这个异常才能回滚,如果是自己封装的异常类,不能回滚。

 

下面是结合数据库操作与Redis,在同一个事务中进行控制的代码样例:

@Transactional
public void test() throws Exception {

        //操作Redis
        redisTemplateTransactional.boundValueOps("name").set("TEST_YYYY");
        redisTemplateTransactional.boundValueOps("age").set("80");

        //操作数据库: JPA保存操作
        SimpleDemoEntity simpleDemoEntity = new SimpleDemoEntity();
        simpleDemoEntity.setRemark("nima");
        simpleDemoEntity.setWeixinId("1000");
        simpleDemoDao.saveAndFlush(simpleDemoEntity);
}

当遇到数据库异常情况,比如,主键冲突,唯一索引值等数据库异常,Redis的操作也可以正常回滚。

原因:

使用@Transactional 事务控制后,Spring对数据库层面进行了完美的事务控制,那么当数据库层抛出的所有异常都可以被事务控制管理。

而Redis或者使用Jedis本身的异常,或者业务异常类并没有被Spring的事务控制管理。
而Spring-redis,给了我们一个可以抛出业务异常的渠道,那就是RedisSystemException。

 

posted @ 2016-03-10 09:49 AnsonYang 阅读(6240) 评论(1) 推荐(2) 编辑
 

2013年3月11日

摘要: VirtualBox VM下安装CentOS6.4 (高手略) 阅读全文
posted @ 2013-03-11 22:44 AnsonYang 阅读(1004) 评论(0) 推荐(0) 编辑
 

2012年5月8日

摘要: name:opensourcelicense key:96189-12042010 00001WEdnZrgimaRUrqqOQkDQFWots XiLKPDX4S5k5SsbtoxGEntWEf3XEAj fK8PTjjGxnr9oWeO3CIq7"tQtPa!Dd 阅读全文
posted @ 2012-05-08 21:59 AnsonYang 阅读(326) 评论(0) 推荐(0) 编辑
 

2011年9月21日

摘要: If you need to build a runnable spring-based JAR using maven, there could be some problems in the spring's configuration parsing. You know, to build a runnable JAR we have to include in the JAR all the dependencies, and Maven can help a lot in this, for example using the maven-assembly-plugin.He 阅读全文
posted @ 2011-09-21 14:59 AnsonYang 阅读(661) 评论(0) 推荐(0) 编辑
 

2011年6月20日

摘要: 参考文件:很完美的权限控制与赋权模块, Spring 真猛~~~http://doc.open-open.com/view/dcc7f4551d144df4b8f2f6750329a535http://www.fengfly.com/document/springsecurity3/ns-config.html 阅读全文
posted @ 2011-06-20 22:59 AnsonYang 阅读(363) 评论(0) 推荐(0) 编辑
 

2011年1月11日

摘要: CSS初探 阅读全文
posted @ 2011-01-11 16:53 AnsonYang 阅读(694) 评论(0) 推荐(0) 编辑
 

2010年12月29日

摘要: 使用了Spring3.0.4一段时间了,总是忙于项目上的分分合合,没有记录期中的过程,到现在反而觉得很空。接下来一段时间,主要是记录Spring3.0.4的特性与使用方式,遇到问题的归纳总结,希望对其他人有所帮助,废话不说,马上开始了。SpringFramework官方网址: SpringFramework 阅读全文
posted @ 2010-12-29 09:51 AnsonYang 阅读(315) 评论(0) 推荐(0) 编辑
 

2010年8月2日

摘要: 项目中使用Spring最新的全Annotation方式,从Controller, Service, 到 DAO全部使用Annotation方式进行开发。在使用@Transactional 事务处理时,遇到了问题:配置如下:web.xml[代码]mvc-config.xml:db-config.xml:[代码]Controller类如下:[代码]ServiceHandler:[代码]DAO:(只提供... 阅读全文
posted @ 2010-08-02 19:27 AnsonYang 阅读(3773) 评论(2) 推荐(3) 编辑
 

2010年7月9日

摘要: 我,一懒人,以前也开过几个博客,写过些文章,但由于懒,懒得想,懒得写,最终都放弃!从大连到了杭州,又杭州回到大连,工作到现在也有5年的时间,觉得自己沉淀的知识很少,学东西老是容易忘,或者有时做事找不出思路来。就想在网上找片地,安个家,把自己的技术生活的一些点滴记录下来,以便后续查阅,也希望利用写作理思路的同时能产生些 Ideas。于是来博客网站申请地盘了。csdn、javaeye、cnblogs都... 阅读全文
posted @ 2010-07-09 11:27 AnsonYang 阅读(271) 评论(8) 推荐(3) 编辑
 
 
点击右上角即可分享
微信分享提示