- 浏览: 202963 次
文章分类
- 全部博客 (131)
- java (22)
- linux (10)
- php (6)
- css (1)
- english (2)
- delphi (7)
- javascript (js) (4)
- 生活 (1)
- sql (1)
- Hibernate (4)
- 面试题 (2)
- android (2)
- socket (1)
- ruby (4)
- jsp (1)
- ph (0)
- 分布式 性能调优 (1)
- 用Nginx (3)
- PHP集群 (1)
- 设计模式 (2)
- 关注 (1)
- 架构师 (2)
- 后台界面 (1)
- mina (1)
- rpc (1)
- 学习 (1)
- shell (4)
- shell 空格 (1)
- Symfony框架 (1)
- svn (2)
- swing (1)
- mysql centos (1)
- 汉字,中文 (1)
- url (1)
- 压缩图 (1)
- 缓存 大型网站 (1)
- linux netstat (1)
- ThreadLocal Connection 事务 (1)
- gwt (1)
- java jetty (1)
- 网站架构 (0)
- 网站架构 淘宝网 (1)
- php yii (1)
- java基础 SCJP (1)
- (1)
- 存储过程 (1)
- 平安PAFA架构 (1)
- ibm (1)
最新评论
-
geek87:
看来起很神奇,,,
在android的spinner中,实现取VALUE值和TEXT值。 -
changkunyang:
呵呵,这个方法不错,试过了,谢谢分享
在android的spinner中,实现取VALUE值和TEXT值。 -
王路喜:
第二种方法中要修改的文件类型是什么啊?我修改了TXT的编码为G ...
ubuntu下eclipse打开window下的java文件,注释内容为乱码,解决方法 -
jxqlovejava:
hashcode前部分,全部来自java核心技术卷1.
关于对象的equal方法和hashCode方法 -
lsf171028:
关于对象的equal方法和hashCode方法
终于运用ThreadLocal实现了Connection和Transaction的管理
http://www.cjsdn.net/post/print?bid=2&id=89152
测试用例(部分)通过
public void testBeginTransaction() throws TransactionException,SQLException {
Transaction tx = null;
try {
tx = ConnectionManager.beginTransaction();
assertEquals("aaa",findProduct16());
updateProduct16();
assertEquals("萝卜",findProduct16());
assertEquals("Alice Mutton",findProduct17());
updateProduct17();
//assertEquals("白菜",findProduct17());
tx.commit();
assertTrue(tx.wasCommitted());
/*
tx.commit();
assertTrue(tx.wasCommitted());
assertEquals("萝卜",findProduct16());
*/
/*
tx.rollback();
assertTrue(tx.wasRolledBack());
assertEquals("aaa",findProduct16());
assertEquals("Alice Mutton",findProduct17());
*/
} catch (TransactionException e) {
tx.rollback();
assertTrue(tx.wasRolledBack());
throw e;
} catch (SQLException e) {
tx.rollback();
assertTrue(tx.wasRolledBack());
//throw new TransactionException(e);
}
assertTrue(ConnectionManager.currentConnection().getAutoCommit());
}
更新了一下
dao.rar (780.49k)
2.Re:终于运用ThreadLocal实现了Connection和Transaction的管理 [Re: wes109] Copy to clipboard
Posted by: wes109
Posted on: 2004-04-23 13:13
主要的管理类
public class ConnectionManager {
private static final ThreadLocal connection = new ThreadLocal();
private static ConnectionFactory factory;
private static boolean initialized = false;
private static final Log log = LogFactory.getLog(ConnectionManager.class);
private ConnectionManager() {
super();
}
private synchronized static void initialize() {
try {
factory = new Configuration().configure().buildConnectionFactory();
} catch (ConfigureException e) {
log.fatal(e);
}
log.info("ConnectionManager initialize success");
initialized = true;
}
public static Connection currentConnection() throws SQLException {
if(!initialized) {
initialize();
}
Connection conn = (Connection)connection.get();
if(conn == null) {
conn = factory.openConnection();
connection.set(conn);
}
return conn;
}
public static void closeConnection() throws SQLException {
Connection conn = (Connection)connection.get();
connection.set(null);
if(conn != null) {
conn.close();
}
}
public static Transaction beginTransaction() throws TransactionException{
Connection conn = null;
try {
conn = currentConnection();
} catch (SQLException e) {
log.error("Can't get current connection with SQLException:",e);
throw new TransactionException("Begin tracsaction fail with SQLException:", e);
}
return new TransactionFactory().beginTransaction(conn);
}
}
http://www.cjsdn.net/post/print?bid=2&id=89152
测试用例(部分)通过
public void testBeginTransaction() throws TransactionException,SQLException {
Transaction tx = null;
try {
tx = ConnectionManager.beginTransaction();
assertEquals("aaa",findProduct16());
updateProduct16();
assertEquals("萝卜",findProduct16());
assertEquals("Alice Mutton",findProduct17());
updateProduct17();
//assertEquals("白菜",findProduct17());
tx.commit();
assertTrue(tx.wasCommitted());
/*
tx.commit();
assertTrue(tx.wasCommitted());
assertEquals("萝卜",findProduct16());
*/
/*
tx.rollback();
assertTrue(tx.wasRolledBack());
assertEquals("aaa",findProduct16());
assertEquals("Alice Mutton",findProduct17());
*/
} catch (TransactionException e) {
tx.rollback();
assertTrue(tx.wasRolledBack());
throw e;
} catch (SQLException e) {
tx.rollback();
assertTrue(tx.wasRolledBack());
//throw new TransactionException(e);
}
assertTrue(ConnectionManager.currentConnection().getAutoCommit());
}
更新了一下
dao.rar (780.49k)
2.Re:终于运用ThreadLocal实现了Connection和Transaction的管理 [Re: wes109] Copy to clipboard
Posted by: wes109
Posted on: 2004-04-23 13:13
主要的管理类
public class ConnectionManager {
private static final ThreadLocal connection = new ThreadLocal();
private static ConnectionFactory factory;
private static boolean initialized = false;
private static final Log log = LogFactory.getLog(ConnectionManager.class);
private ConnectionManager() {
super();
}
private synchronized static void initialize() {
try {
factory = new Configuration().configure().buildConnectionFactory();
} catch (ConfigureException e) {
log.fatal(e);
}
log.info("ConnectionManager initialize success");
initialized = true;
}
public static Connection currentConnection() throws SQLException {
if(!initialized) {
initialize();
}
Connection conn = (Connection)connection.get();
if(conn == null) {
conn = factory.openConnection();
connection.set(conn);
}
return conn;
}
public static void closeConnection() throws SQLException {
Connection conn = (Connection)connection.get();
connection.set(null);
if(conn != null) {
conn.close();
}
}
public static Transaction beginTransaction() throws TransactionException{
Connection conn = null;
try {
conn = currentConnection();
} catch (SQLException e) {
log.error("Can't get current connection with SQLException:",e);
throw new TransactionException("Begin tracsaction fail with SQLException:", e);
}
return new TransactionFactory().beginTransaction(conn);
}
}
发表评论
-
java Swing组件大全超牛的例子
2013-02-18 16:15 13416http://www.blogjava.net/tjsjrx2 ... -
Java ClassLoader
2012-12-26 21:27 771http://blog.sina.com.cn/s/blog_ ... -
Java运行环境JRE和Java虚拟机JVM有什么关系?
2012-10-15 17:41 1074http://ghost1193.blog.163.com/b ... -
JMX 平台MXBean
2012-10-15 17:39 1955from http://1985wanggang.blog.1 ... -
linux脚本(让JAR的config放在外边)
2012-08-09 20:15 1673linux脚本(让JAR的config放在外边) http: ... -
java压缩后高清晰图
2012-07-03 17:28 1342imgscalr – Java Image Scaling L ... -
proxy代理
2012-02-07 18:01 663http://hi.baidu.com/a8431256/bl ... -
使用Java开发需要关注的那些事儿
2011-11-22 14:35 681http://blog.csdn.net/jollyant/a ... -
spring3mvc+hibernate包
2011-07-06 15:30 1241===== spring3 ============ org. ... -
spring3+ ibatis2()配置
2011-07-06 10:30 1122参考 http://blog.csdn.net/cl61917 ... -
tomcat6 + apache2 集群配置
2011-07-04 14:45 910参考 http://www.iteye.com/top ... -
web.xml listener、filter、servlet 加载顺序
2010-11-28 15:25 1037web.xml listener、filter、servlet ... -
关于对象的equal方法和hashCode方法
2010-09-14 12:27 1407from http://gumeng000.iteye.com ... -
将中文转换成unicode的工具:
2010-08-11 17:49 1700from http://hechengrong.iteye. ... -
java 问题收集
2010-07-23 16:09 6861 StringBuffer / StringBuilder ... -
html
2010-05-14 12:14 7621 <input type="text&quo ... -
web错误代码
2010-03-16 12:22 8671 http status 404 这个页路径或**.js ... -
struts 提示错误收集
2009-10-14 17:20 9881 End of content reached whil ... -
javascript单引号和双引号区别
2009-09-09 18:06 2554javascript单引号和双引号区别2009-08-27 ... -
struts1(html:checkbox)
2009-07-31 16:16 1132<html:checkbox property=&quo ...
相关推荐
主要介绍ThreadLocal的原理,实例分析以及注意事项
本例以序列号生成的程序为例,展示ThreadLocal的使用
ThreadLocal入门教程。 讲解了线程安全和ThreadLocal的使用的基本知识。
ThreadLocal
ThreadLocal源码分析和使用
理解ThreadLocal 理解ThreadLocal 理解ThreadLocal 理解ThreadLocal
NULL 博文链接:https://qixiaopeng.iteye.com/blog/551764
使用c3p0数据源,进行转账操作的小型简单练习;使用ThreadLocal控制事务中的connection唯一,实现mvc三层结构互不干扰。
ThreadLocal应用示例及理解,这个写了相关的示例,可以参考一下。
JDBC事务的封装和Threadlocal实例,参考博客:http://blog.csdn.net/daijin888888/article/details/50988053
通过ThreadLocal对象定位到线程:Thread.currentThread()通过ThreadLocal对象拿到所在的ThreadLocalMap: T
详解java底层实现原理,ThreadLocal底层实现的数据结构,为什么不会导致内存泄露
学习ThreadLocal,了解其中的原理,以及学习其中的优点!避免坑点!!
在C层中常常会再次分层,如:servlet(web层)、service(业务逻辑层)、dao(数据访问层),其中service和dao最容易混在一起,如转钱交易场景,service层需要执行“事务”操作,会用到数据库连接对象(Connection)...
Synchronized与ThreadLocal
正确理解ThreadLocal.pdf
DbUTils中用ThreadLocal类
AopLogAopLog是基于SpringAop和ThreadLocal实现的一个对请求方法埋点记录与处理的日志工具包。设计目的和场景:使用Spring Aop拦截程序,基本上都是同一个小异,不想日后每个项目都柏林都写一份这样的Aop拦截处理...
MVC框架,加注解,Struts框架的思想,动态代理,线程管理对象ThreadLocal,Connection对象池,Properties文件读取,EL表达式,JSTL,JavaBean,Java访问MySQL数据库,增删改查... ---------------------------------...