spring 事物 线程池 测试
2016-03-10 16:05
411 查看
static ExecutorService executorService = Executors.newFixedThreadPool(1); public List<Map<String, Object>> queryForListMap(final String sql) throws Exception { /*new Thread(){ @Override public void run() { //使用当前的 transaction List<Map<String, Object>> list=jdbcTemplate.queryForList(sql); System.out.println(list.size()); } }.start();*/ /*new Thread(){ @Override public void run() { List<Map<String, Object>> list=null; try { //使用当前的 transaction list = queryForListMap2(sql); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println(list.size()); } }.start();*/ /*new Thread(){ @Override public void run() { //Creating new transaction with name serviceUtil.queryTasks(); } }.start();*/ /*for(int i=0;i<8;i++){ executorService.execute(new Runnable() { @Override public void run() { //使用当前的 transaction List<Map<String, Object>> list=jdbcTemplate.queryForList(sql); System.out.println(list.size()); System.out.println("ThreadId==="+Thread.currentThread().getId()); //Creating new transaction with name //每一次Creating new transaction serviceUtil.queryTasks(); } }); }*/ /*for(int i=0;i<4;i++){ executorService.submit(new Callable<String>() { public String call() throws Exception { //使用当前的 transaction //List<Map<String, Object>> list=jdbcTemplate.queryForList(sql); //System.out.println(list.size()); System.out.println("ThreadId==="+Thread.currentThread().getId()); //Creating new transaction with name //每一次Creating new transaction serviceUtil.queryTasks(); return sql; }; }); }*/ for(int i=0;i<4;i++){ executorService.submit(new Callable<String>() { public String call() throws Exception { System.out.println("ThreadId==="+Thread.currentThread().getId()); //Creating new transaction with name //同一个线程缓存的同一个connection //每一次Creating new transaction serviceUtil.queryTasks(); return sql; }; }); } //Connection conn = DataSourceUtils.getConnection(jdbcTemplate.getDataSource()); /*for(int i=0;i<8;i++){ //使用当前的 transaction serviceUtil.queryTasks(); }*/ System.out.println("threadId==="+Thread.currentThread().getId()); return jdbcTemplate.queryForList(sql); }
/** * Actually obtain a JDBC Connection from the given DataSource. * Same as {@link #getConnection}, but throwing the original SQLException. * <p>Is aware of a corresponding Connection bound to the current thread, for example * when using {@link DataSourceTransactionManager}. Will bind a Connection to the thread * if transaction synchronization is active (e.g. if in a JTA transaction). * <p>Directly accessed by {@link TransactionAwareDataSourceProxy}. * @param dataSource the DataSource to obtain Connections from * @return a JDBC Connection from the given DataSource * @throws SQLException if thrown by JDBC methods * @see #doReleaseConnection */ public static Connection doGetConnection(DataSource dataSource) throws SQLException { Assert.notNull(dataSource, "No DataSource specified"); ConnectionHolder conHolder = (ConnectionHolder) TransactionSynchronizationManager.getResource(dataSource); if (conHolder != null && (conHolder.hasConnection() || conHolder.isSynchronizedWithTransaction())) { conHolder.requested(); if (!conHolder.hasConnection()) { logger.debug("Fetching resumed JDBC Connection from DataSource"); conHolder.setConnection(dataSource.getConnection()); } System.out.println("threadId==="+Thread.currentThread().getId()); return conHolder.getConnection(); } // Else we either got no holder or an empty thread-bound holder here. logger.debug("Fetching JDBC Connection from DataSource"); Connection con = dataSource.getConnection(); if (TransactionSynchronizationManager.isSynchronizationActive()) { logger.debug("Registering transaction synchronization for JDBC Connection"); // Use same Connection for further JDBC actions within the transaction. // Thread-bound object will get removed by synchronization at transaction completion. ConnectionHolder holderToUse = conHolder; if (holderToUse == null) { holderToUse = new ConnectionHolder(con); } else { holderToUse.setConnection(con); } holderToUse.requested(); TransactionSynchronizationManager.registerSynchronization( new ConnectionSynchronization(holderToUse, dataSource)); holderToUse.setSynchronizedWithTransaction(true); if (holderToUse != conHolder) { TransactionSynchronizationManager.bindResource(dataSource, holderToUse); } } return con; }
spring事物在线程池中,由于线程对象都是缓存对象,会长期持有数据库连接connection对象。
相关文章推荐
- Spring单实例、多线程安全、事务解析
- Description Resource Path Location Type Java compiler level does not match the v
- Java命令的同时添加依赖jar包及参数
- spring 事物日志
- IT十八掌作业_java基础第七天_匿名内部类、异常、包和jar
- Spring数据库访问之ORM(三)
- java二维码生成及解析代码
- Struts2自定义拦截器
- Spring数据库访问之ORM(二)
- JAVA boolean 类型
- Spring数据库访问之ORM(一)
- 做一个合格的程序猿之浅析Spring IoC源码(五)再谈BeanPostProcessor(2)
- JAVA char类型
- eclipse下用maven插件+Mabatis-generator生成mybatis的文件
- Spring声明式事务配置管理方法
- JAVA常量
- Java实现AES加密,异常java.security.InvalidKeyException: Illegal key size 的解决
- java对象和json对象之间互相转换
- Spring读书笔记-----Spring的Bean之Bean的基本概念
- handler method 参数绑定常用的注解