spring 事物 线程池 测试

124 篇文章 1 订阅
	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对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值