spring 事物 线程池 测试

来源:互联网 发布:car软件 编辑:程序博客网 时间:2024/05/22 12:19
static  ExecutorService executorService = Executors.newFixedThreadPool(1);public List<Map<String, Object>> queryForListMap(final String sql)throws Exception {/*new Thread(){@Overridepublic void run() {    //使用当前的 transactionList<Map<String, Object>>  list=jdbcTemplate.queryForList(sql);System.out.println(list.size());}}.start();*//*new Thread(){@Overridepublic void run() {List<Map<String, Object>> list=null;try {        //使用当前的 transactionlist = queryForListMap2(sql);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}System.out.println(list.size());}}.start();*//*new Thread(){@Overridepublic void run() {//Creating new transaction with nameserviceUtil.queryTasks();}}.start();*//*for(int i=0;i<8;i++){executorService.execute(new Runnable() {@Overridepublic void run() { //使用当前的 transactionList<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 transactionserviceUtil.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 transactionserviceUtil.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 transactionserviceUtil.queryTasks();return sql;};});}//Connection conn = DataSourceUtils.getConnection(jdbcTemplate.getDataSource());/*for(int i=0;i<8;i++){//使用当前的 transactionserviceUtil.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对象。

0 0
原创粉丝点击