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

扫描二维码关注公众号,回复: 491410 查看本文章

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.

share improve this question
 

1 Answer

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

share improve 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 

猜你喜欢

转载自kanpiaoxue.iteye.com/blog/2265018