`
kanpiaoxue
  • 浏览: 1781684 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

SimpleJdbcInsert异常:InvalidDataAccessApiUsageException: Configuration can't

 
阅读更多

 

今天使用 org.springframework.jdbc.core.simple.SimpleJdbcInsert来进行一些操作,代码如下:

 

@Repository("auditLogDao")
public class AuditLogDaoImpl implements AuditLogDao {
    private static final Logger LOGGER = LoggerFactory.getLogger(AuditLogDaoImpl.class);
    @Autowired
    private SimpleJdbcInsert simpleJdbcInsert;

    /*
     * (non-Javadoc)
     */
    @Override
    public void add(EntityOperationAuditLog entityOperationAuditLog) {
        Preconditions.checkNotNull(entityOperationAuditLog);
        if (null == entityOperationAuditLog.getGmtCreate()) {
            entityOperationAuditLog.setGmtCreate(new Date());
        }
        LOGGER.debug("start insert tb_operation_audit_log {} ", entityOperationAuditLog);

        // 表名
        simpleJdbcInsert.withTableName("tb_operation_audit_log");
        SqlParameterSource parameters = new BeanPropertySqlParameterSource(entityOperationAuditLog);
        // 插入db并返回自增主键id
        simpleJdbcInsert.execute(parameters);
    }
}

 

该代码执行第一次的时候是很好的,可是执行第二次的时候就报错了,如下:

org.springframework.dao.InvalidDataAccessApiUsageException: Configuration can't be altered once the class has been compiled or used

 然后去查了查,发现这个SimpleJdbcInsert只能指定一次tableName,不能为同一个SimpleJdbcInsert的实例多次设置tableName。所以才引起上面的异常信息。

改为如下的代码就解决了问题:

@Repository("auditLogDao")
public class AuditLogDaoImpl implements AuditLogDao {
    private static final Logger LOGGER = LoggerFactory.getLogger(AuditLogDaoImpl.class);
    
    private SimpleJdbcInsert simpleJdbcInsert;
    
    
    public AuditLogDaoImpl() {
        super();
    }

    @Autowired
    public AuditLogDaoImpl(SimpleJdbcInsert simpleJdbcInsert) {
        super();
        this.simpleJdbcInsert = simpleJdbcInsert;
        simpleJdbcInsert.withTableName("tb_operation_audit_log");
    }

    /*
     * (non-Javadoc)
     * @see 
     * EntityOperationAuditLog)
     */
    @Override
    public void add(EntityOperationAuditLog entityOperationAuditLog) {
        Preconditions.checkNotNull(entityOperationAuditLog);
        if (null == entityOperationAuditLog.getGmtCreate()) {
            entityOperationAuditLog.setGmtCreate(new Date());
        }
        LOGGER.debug("start insert tb_operation_audit_log {} ", entityOperationAuditLog);

        // 表名
        // simpleJdbcInsert.withTableName("tb_operation_audit_log");
        SqlParameterSource parameters = new BeanPropertySqlParameterSource(entityOperationAuditLog);
        // 插入db并返回自增主键id
        simpleJdbcInsert.execute(parameters);
    }
}

 

 

 

参考: http://stackoverflow.com/questions/15606588/org-springframework-dao-invaliddataaccessapiusageexception-configuration-cant

 

Iam inserting some data into DB with simpleJdbcInsert in spring , it works fine for first step (i mean for first insertion ) , when i try yo save the data for second time iam getting exception as :org.springframework.dao.InvalidDataAccessApiUsageException: Configuration can't be altered once the class has been compiled or used."

Can any one help me out in this.

shareimprove this question
 

This exception usually happens when you try to config(again) a compiled simpleJdbcInsert.

compiled means you have instantiated a simpleJdbcInsert instance and set up data source andtable name already. Once an simpleJdbcInsert instance is compiled, you should not re-config it again; for instance, set another table name. Create a new simpleJdbcInsert instace if you need to do so.

To get a comprehensive understanding about how simpleJdbcInsert works, take a look into source code of simpleJdbcInsert and AbstractJdbcInsert. especially the method compile() inAbstractJdbcInsert.java

shareimprove this answer
 
    
Hi cannot i insert more records being on the same page, on first insertion data is getting inserted , but after navigating and again coming to insertion page , this kind of error iam getting. could u please tell is thier any configureation required to insert more records – Renukeswar Mar 30 '13 at 2:33
    
are you inserting the the data into same table after page page navigation? Please attache your code to let people reproduce the issue and figure out where goes wrong. – spiritwalker Mar 30 '13 at 2:57
    
@Resource(name="dataSource") public void setSimpleJdbcTemplate(DataSource dataSource) { this.simpleJdbcTemplate = new SimpleJdbcTemplate(dataSource); and other paramertes set into Map, simpleJdbcInsert.execute(parameterMap). this.simpleJdbcInsert = new SimpleJdbcInsert(dataSource); } code to insert into DB: this.simpleJdbcInsert.withTableName("MOVIE_INFO"); Map<String,Object> parameterMap = new HashMap<String,Object>(); parameterMap.put("MOVIE_NAME", movie.getMovieName()); – Renukeswar Mar 30 '13 at 4:53 
    
did any one come across this kind of issue. I saw AbstractJdbcInsert ,in which isCompile method checks for this is compile variable is set true this exception is thrown – Renukeswar Mar 30 '13 at 10:33
    
remove ** this.simpleJdbcInsert.withTableName("MOVIE_INFO");**. withTableName does nothing rather than just setting table name. And please refer to my answer, you cannot set table name multiple times to same SimpleJdbcInsert instance. – spiritwalker Mar 30 '13 at 11:47 

 

 

 

分享到:
评论

相关推荐

    基于net的超市管理系统源代码(完整前后端+sqlserver+说明文档+LW).zip

    功能说明: 环境说明: 开发软件:VS 2017 (版本2017以上即可,不能低于2017) 数据库:SqlServer2008r2(数据库版本无限制,都可以导入) 开发模式:mvc。。。

    LABVIEW程序实例-公式节点.zip

    labview程序代码参考学习使用,希望对你有所帮助。

    大米商城开源版damishop(适合外贸)

    大米外贸商城系统 简称damishop 完全开源版,只需做一种语言一键开启全球133中语言自动翻译功能,价格实现自动汇率转换,集成微信支付宝 paypal以及国外主流支付方式,自带文章博客系统。 软件架构 基于MVC+语言包模式,增加控制台,API导入产品方便对接其他系统(带json示例数据)。 使用要求 PHP7.4+ MYSQL5.6+ REDIS(可选) 安装方法 composer install 打开安装向导安装 http://您的域名/install 特色 1、缓存层增加时间与批量like删除 2、API产品导入方便对接其他系统 3、增加控制台命令行,命令行生成语言翻译包 4、后台一键开启自动翻译模式,支持全球133中语言,由于google代理翻译需要收费,这个功能需要付费。 5、可选购物车与ajax修改购物车产品 6、一键结算checkout 7、增加网站前台自定义路由 方便seo 更新日志 v3.9.7 集成鱼码支付接口,方便个人站长即使收款到账使用 v3.9.3 更新内容 1:增加ueditor与旧编辑器切换 2:增加可视化布局插

    LABVIEW程序实例-通过全局变量接收数据.zip

    labview程序代码参考学习使用,希望对你有所帮助。

    LABVIEW程序实例-日历控件.zip

    labview程序代码参考学习使用,希望对你有所帮助。

    毕设和企业适用springboot人工智能客服系统类及旅游规划平台源码+论文+视频.zip

    毕设和企业适用springboot人工智能客服系统类及旅游规划平台源码+论文+视频

Global site tag (gtag.js) - Google Analytics