NutzCN Logo
问答 不能在其它线程中使用MainSetup中的dao?
发布于 1970天前 作者 qq_8c799e25 1654 次浏览 复制 上一个帖子 下一个帖子
标签:

MainSetup 初始化了一个静态dao, 后续启动了 NettyServer实例

public class MainSetup implements Setup{

	
	private static final Log log = Logs.get();
	
	public static Dao dao = null;

	@Override
	public void init(NutConfig conf) {
		// TODO Auto-generated method stub
		Ioc ioc = conf.getIoc();
		MainSetup.dao = ioc.get(Dao.class);
                。。。
                NettyServer.getInstance();
}

}

然后我在NettyServer的接口回调中直接使用这个静态dao

	@Override
	public void channel_active(String channel_id, String client_id, String client_type) {
		
		ImClientBean b = new ImClientBean(channel_id, client_id, client_type);
		b.setIs_active(true);
		ImClientBean t = MainSetup.dao.fetch(b);
		if(t==null){
			MainSetup.dao.insert(b);
		}else{
			MainSetup.dao.update(b);
		}
	}

直接报错:

2019-07-31 16:58:44,992 INFO  55 - NettyServerHandler - exceptionCaught:
org.nutz.dao.DaoException: java.lang.RuntimeException: r u kidding me?! It is impossible!
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:140)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:93)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:82)
	at org.nutz.dao.impl.DaoSupport.run(DaoSupport.java:240)
	at org.nutz.dao.impl.DaoSupport._exec(DaoSupport.java:252)
	at org.nutz.dao.impl.NutDao.fetch(NutDao.java:617)
	at com.hans.whook.im.ImMsgReceiver.channel_active(ImMsgReceiver.java:61)
	at com.hans.whook.im.NettyServerHandler.channelRead(NettyServerHandler.java:146)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352)
	at io.netty.handler.timeout.IdleStateHandler.channelRead(IdleStateHandler.java:287)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352)
	at io.netty.handler.codec.ByteToMessageDecoder.fireChannelRead(ByteToMessageDecoder.java:321)
	at io.netty.handler.codec.ByteToMessageDecoder.channelRead(ByteToMessageDecoder.java:295)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
	at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:352)
	at io.netty.channel.DefaultChannelPipeline$HeadContext.channelRead(DefaultChannelPipeline.java:1408)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:374)
	at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:360)
	at io.netty.channel.DefaultChannelPipeline.fireChannelRead(DefaultChannelPipeline.java:930)
	at io.netty.channel.nio.AbstractNioByteChannel$NioByteUnsafe.read(AbstractNioByteChannel.java:163)
	at io.netty.channel.nio.NioEventLoop.processSelectedKey(NioEventLoop.java:697)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeysOptimized(NioEventLoop.java:632)
	at io.netty.channel.nio.NioEventLoop.processSelectedKeys(NioEventLoop.java:549)
	at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:511)
	at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:918)
	at io.netty.util.internal.ThreadExecutorMap$2.run(ThreadExecutorMap.java:74)
	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
	at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: r u kidding me?! It is impossible!
	at org.nutz.lang.Lang.impossible(Lang.java:120)
	at org.nutz.dao.impl.sql.pojo.SingleColumnCondtionPItem.joinParams(SingleColumnCondtionPItem.java:61)
	at org.nutz.dao.impl.jdbc.NutPojo.getParamMatrix(NutPojo.java:82)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runSelect(NutDaoExecutor.java:215)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:53)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:66)
	at org.nutz.dao.impl.interceptor.DaoLogInterceptor.filter(DaoLogInterceptor.java:22)
	at org.nutz.dao.DaoInterceptorChain.doChain(DaoInterceptorChain.java:64)
	at org.nutz.dao.DaoInterceptorChain.invoke(DaoInterceptorChain.java:139)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.runCallback(NutDaoRunner.java:159)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:126)
	... 32 more

我该如何初始化这个dao呢

4 回复

是ImClientBean没有主键,并不是dao初始化的问题


@Table("im_client_bean") @PK({"client_id"}) public class ImClientBean { @Column("client_id") protected String client_id;// @Column("client_type") protected String client_type;// @Column("channel_id") protected String channel_id;// channel_id @Column("is_active") protected boolean is_active;// 是否active @Column("use_ts") protected long use_ts;// 上一次使用时间, public ImClientBean(){ } public ImClientBean(String channel_id, String client_id, String client_type) { // TODO Auto-generated constructor stub this(); this.channel_id = channel_id; this.client_id = client_id; this.client_type = client_type; this.is_active = false; this.use_ts = 0L; } public String getClient_id() { return client_id; } public void setClient_id(String client_id) { this.client_id = client_id; } public String getClient_type() { return client_type; } public void setClient_type(String client_type) { this.client_type = client_type; } public String getChannel_id() { return channel_id; } public void setChannel_id(String channel_id) { this.channel_id = channel_id; } public boolean isIs_active() { return is_active; } public void setIs_active(boolean is_active) { this.is_active = is_active; } public long getUse_ts() { return use_ts; } public void setUse_ts(long use_ts) { this.use_ts = use_ts; } }

有主键啊,数据库查看表也是有主键

SingleColumnCondtionPItem里面出的异常:

    public int joinParams(Entity<?> en, Object obj, Object[] params, int off) {
        // 默认值可以直接使用
        if (def == obj && null != obj) {
            params[off++] = def;
        }
        // 进行更精细的判断...
        else {
            en = _en(en);
            // 是个实体对象,试图直接取值
            if (null != obj && null != mf && mf.getEntity() == en && en.getType().isInstance(obj))
                params[off++] = mf.getValue(obj);
            // 采用默认值
            else if (null != def)
                params[off++] = def;
            // 试图转换传入的对象
            else if (null != obj) {
                // TODO 这是啥规则?!!! 完全搞不懂!!!
                params[off++] = Castors.me().castTo(obj, colType);
            }
            // 逼急了,老子抛异常了!
            else
                throw Lang.impossible(); //异常
        }
        return off;
    }

我把ImClientBean 的主键 改用Name 也不行, 1.r65和 1.r67 都是一样的错,是bug吗?

不是bug,dao.fetch的时候主键不能是null

添加回复
请先登陆
回到顶部