NutzCN Logo
问答 nutz 连接oracle 自增字段不自增,一直在new时赋值为0
发布于 2639天前 作者 qq_e79520c3 1838 次浏览 复制 上一个帖子 下一个帖子
标签:

private static final long serialVersionUID = 1L;
@Column
@Id
@Prev({
// //仅做演示,实际使用oracle时,请使用触发器+序列的方式实现自增长ID,否则高并发下此种写法性能是个瓶颈
// //实际上不推荐在主键上使用自定义sql来生成
@SQL(db = DB.ORACLE, value = "SELECT SYS_LOG_S.nextval FROM dual")
})
private long id;

20 回复

什么版本的nutz?

刚连接oracle的时候,不报错的

日志贴一下, 用"插入代码或日志片段"按钮.

不好意思,应为我同事在用触发器+序列解决的原因,所以无法还原日志 ,但是主要错误是这个,

!Nutz SQL Error: 'INSERT INTO sys_log(id,username,type,tag,src,ip,msg,opBy,opAt,delFlag) VALUES(0,'超级管理员','info','用户登陆','cn.wizzer.app.web.modules.controllers.platform.sys.SysLoginController#doLogin','0:0:0:0:0:0:0:1','成功登录系统!','bae9ac75d26741f2a914eeb152a2c94f',1503567873,'NULL') '

用fastInsert执行??

[INFO ] 2017-08-24 17:49:39,733 cn.wizzer.app.web.commons.slog.SLogService.sync(SLogService.java:46) - insert syslog sync fail
org.nutz.dao.DaoException: !Nutz SQL Error: 'INSERT INTO sys_log(id,username,type,tag,src,ip,msg,opBy,opAt,delFlag) VALUES(0,'超级管理员','info','用户登陆','cn.wizzer.app.web.modules.controllers.platform.sys.SysLoginController#doLogin','0:0:0:0:0:0:0:1','成功登录系统!','bae9ac75d26741f2a914eeb152a2c94f',1503568179,'NULL') '
PreparedStatement: 

恩恩,

    public void async(Sys_log syslog) {
        LinkedBlockingQueue<Sys_log> queue = this.queue;
        if (queue != null)
            try {
                boolean re = queue.offer(syslog, 50, TimeUnit.MILLISECONDS);
                if (!re) {
                    log.info("syslog queue is full, drop it ...");
                }
            } catch (InterruptedException e) {
            }
    }

    public void sync(Sys_log syslog) {
        try {
            dao.fastInsert(syslog);
        } catch (Throwable e) {
            log.info("insert syslog sync fail", e);
        }
    }

    public void run() {
        while (true) {
            LinkedBlockingQueue<Sys_log> queue = this.queue;
            if (queue == null)
                break;
            try {
                Sys_log sysLog = queue.poll(1, TimeUnit.SECONDS);
                if (sysLog != null) {
                    sync(sysLog);
                }
            } catch (InterruptedException e) {
                break;
            }
        }

dao.fastInsert(syslog);

那必须用触发器, 然后删掉@Prev

好的,谢谢了,这个触发器我的看看怎么弄

	at java.lang.Thread.run(Thread.java:748)
[INFO ] 2017-08-24 18:12:38,897 cn.wizzer.app.web.commons.slog.SLogService.sync(SLogService.java:46) - insert syslog sync fail
org.nutz.dao.DaoException: !Nutz SQL Error: 'INSERT INTO sys_log(username,type,tag,src,ip,msg,opBy,opAt,delFlag) VALUES('超级管理员','info','用户登陆','cn.wizzer.app.web.modules.controllers.platform.sys.SysLoginController#doLogin','0:0:0:0:0:0:0:1','成功登录系统!','bae9ac75d26741f2a914eeb152a2c94f',1503569558,'NULL') '
PreparedStatement: 
'INSERT INTO sys_log(username,type,tag,src,ip,msg,opBy,opAt,delFlag) VALUES(?,?,?,?,?,?,?,?,?) '
CaseMessage=ORA-04098: 触发器 'ZSXXUSER.TRIGGER_SYS_LOG' 无效且未通过重新验证

	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:104)
	at org.nutz.plugins.cache.dao.CachedNutDaoExecutor._exec(CachedNutDaoExecutor.java:317)
	at org.nutz.plugins.cache.dao.CachedNutDaoExecutor.exec(CachedNutDaoExecutor.java:187)
	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:158)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._runWithoutTransaction(NutDaoRunner.java:125)
	at org.nutz.dao.impl.sql.run.NutDaoRunner._run(NutDaoRunner.java:92)
	at org.nutz.dao.impl.sql.run.NutDaoRunner.run(NutDaoRunner.java:81)
	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.EntityOperator.exec(EntityOperator.java:55)
	at org.nutz.dao.impl.NutDao.fastInsert(NutDao.java:192)
	at cn.wizzer.app.web.commons.slog.SLogService.sync(SLogService.java:44)
	at cn.wizzer.app.web.commons.slog.SLogService.run(SLogService.java:58)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.run(FutureTask.java:266)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLSyntaxErrorException: ORA-04098: 触发器 'ZSXXUSER.TRIGGER_SYS_LOG' 无效且未通过重新验证

	at oracle.jdbc.driver.SQLStateMapping.newSQLException(SQLStateMapping.java:91)
	at oracle.jdbc.driver.DatabaseError.newSQLException(DatabaseError.java:133)
	at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:206)
	at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:455)
	at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:413)
	at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:1034)
	at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:194)
	at oracle.jdbc.driver.T4CPreparedStatement.executeForRows(T4CPreparedStatement.java:953)
	at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1222)
	at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3387)
	at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3488)
	at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1374)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2931)
	at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
	at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2929)
	at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:131)
	at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:493)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor._runPreparedStatement(NutDaoExecutor.java:308)
	at org.nutz.dao.impl.sql.run.NutDaoExecutor.exec(NutDaoExecutor.java:90)
	... 21 more

boss,这个触发器什么情况

触发器没写好吧

手动在客户端里执行 SQL 能插入吗?

来自美丽的 NutzCN

2,创建序列(实现主键自增的必要前提) 
Java代码  
--创建序列(实现person,address主键自增的必要序列)  
create sequence seq_nutz  
increment by 1  
start with 1  
maxvalue 999999999;  


3,给要主键自增的表上创建触发器(在插入前利用触发器给主键id赋值) 
Java代码  
--给person表创建触发器--实现主键自增  
CREATE TRIGGER trigger_pk_person BEFORE  
insert ON  T_PERSON FOR EACH ROW  
begin  
select seq_nutz.nextval into:New.id from dual;  
end;  

数据库显示成功

把sql拷贝执行一下, 肯定也是报错

可以把表删了, 让nutz来建表

这个方法也试了,第一次插入一条日志数据id =0,后面再插入数据就一直是id =0

删表,让nutz建表

不知道你的触发器写错什么,但我知道nutz建的话是肯定对的

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