close() called when transaction is OPEN 一般思路

本文探讨了在自定义Flume Sink过程中遇到的“close() called when transaction is OPEN”错误,分析了可能的原因,并提供了一段示例代码来说明如何避免此类问题的发生。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

自定义flume  sink 操作的时候,往往可能报这样的错

close() called when transaction is OPEN    ,   close() called when transaction is begin()  等,什么原因造成的???

我也说不清楚,一般是在sink的transaction开始时,处理获取的event,但是没有处理完,又有新的Transaction开始

造成的线程间的干扰吧。也不是很明白,这个错误有时候报,有时候又没了。。。

看一段代码。

Transaction tx = null ;	
		Status status=null;
		logger.debug("开始一个Transaction");
		Channel channel= getChannel();
			try{
				tx= channel.getTransaction();
				tx.begin();
				for(int i=0;i<batchSize;i++)
				{
					// 使用take方法尽可能的以批量的方式从Channel中读取事件,直到没有更多的事件
					Event event =  channel.take();
					if(event==null)
					{
						break;
					}
					else{  // 也可以不需要else
						byte[] body = event.getBody();
						String str=new String(body);
						logger.info("********************开始插入******");
						this.storeMongo(str);				
					}
				}
				tx.commit();
				status=Status.READY;
				}catch (Exception e) {
		            logger.error("can't process events, drop it!", e);
		            if (tx != null) {
		                tx.commit();// commit to drop bad event, otherwise it will enter dead loop.
		            }
		        } finally {
		            if (tx != null) {
		            	logger.debug("结束一个Transaction");
		                tx.close();
		            }
		        }
			return status;

我的只能是这样的了,或许有更好的形式,但是一般的结构都这样,也没有固定的标准。 这样的写法是开启一个Transaction后,可以从 Channel中获取较多的event,看source获取的数目。

不敢保证上面的那个错误再出现,我是在虚拟机上没问题,到服务器就出现上面的问题的。。。尴尬噻


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值