2017.7.7 postgreSQL在插入造成重复时执行更新

参考来自:https://stackoverflow.com/questions/1109061/insert-on-duplicate-update-in-postgresql/1109198#1109198

功能需求:接口定义中包含字段update,当它为true时,批量插入中有记录造成唯一键重复时,不报异常,而是执行更新。

1.接口定义

 1 {
 2    "resources":
 3    [
 4       {
 5        "name":"*",
 6        "display_name": "*",
 7        "description": "*",
 8        "service": "*"
 9       },
10       ....
11    ],
12  "update":"true"
13 
14 }

2.代码

原本是通过报的异常 DuplicateKeyException,在catch语句里执行update。但是这样操作有一个问题,后面单独说。

 1             try {
 2                 resourceDao.insert(resource); 5             } catch (DuplicateKeyException e) {
 6 //                if (StringUtils.equals(update,Constants.KEY_FALSE)) {
 7 //                    throw new AuthServiceException(ErrorCode.RESOURCE_DUPLICATE_KEY, new Object[]{resource.getFdResName()}, e);
 8 //                }else if (StringUtils.equals(update,Constants.KEY_TRUE)){
 9 //                    resourceDao.update(resource);
10 //                }
11                 throw new AuthServiceException(ErrorCode.RESOURCE_DUPLICATE_KEY, new Object[]{resource.getFdResName()}, e);
12              } catch (Exception e) {
13                 throw new AuthServiceException(ErrorCode.RESOURCE_CREATE_FAILED, new Object[]{resource.getFdResName()}, e);
14             }

3.SQL语句

示例语句:

1 INSERT INTO the_table (id, column_1, column_2) 
2 VALUES (1, 'A', 'X')3 ON CONFLICT (唯一键) DO UPDATE 
4 SET column_1 ='A', 
5     column_2 = 'X';

完整语句:

 1     <insert id="insert" parameterType="Resource" useGeneratedKeys="true" keyProperty="fdResid">
 2         INSERT INTO t_resource
 3         <trim prefix="(" suffix=")" suffixOverrides=",">
 4             <if test="fdResName != null">
 5                 fd_res_name,
 6             </if>
 7             <if test="fdDisplayName != null">
 8                 fd_display_name,
 9             </if>
10             <if test="fdResDesc != null">
11                 fd_res_desc,
12             </if>
13             <if test="fdTenantId != null">
14                 fd_tenantid,
15             </if>
16             <if test="fdService != null">
17                 fd_service,
18             </if>
19         </trim>
20         <trim prefix="values (" suffix=")" suffixOverrides=",">
21             <if test="fdResName != null">
22                 #{fdResName,jdbcType=VARCHAR},
23             </if>
24             <if test="fdDisplayName != null">
25                 #{fdDisplayName,jdbcType=VARCHAR},
26             </if>
27             <if test="fdResDesc != null">
28                 #{fdResDesc,jdbcType=VARCHAR},
29             </if>
30             <if test="fdTenantId != null">
31                 #{fdTenantId,jdbcType=INTEGER},
32             </if>
33             <if test="fdService != null">
34                 #{fdService,jdbcType=VARCHAR},
35             </if>
36         </trim>
37 
38         <if test="fdUpdate == 'true'">
39             ON CONFLICT(fd_res_name, fd_tenantid, fd_service) DO UPDATE
40             <set>
41                 <if test="fdDisplayName != null">fd_display_name = #{fdDisplayName},</if>
42                 <if test="fdResDesc != null">fd_res_desc = #{fdResDesc},</if>
43                 <if test="fdService != null">fd_service = #{fdService},</if>
44                 <if test="fdModifyDate != null">
45                     fd_modify_date = #{fdModifyDate,jdbcType=VARCHAR},
46                 </if>
47                 <if test="fdModifyPerson != null">
48                     fd_modify_person = #{fdModifyPerson,jdbcType=VARCHAR},
49                 </if>
50             </set>
51         </if>
52     </insert>

4.遇到的问题

之前提到过,之前的处理是通过所报异常信息来做的。如果重复了,在catch语句里执行update。

1              catch (DuplicateKeyException e) {
2                 if (StringUtils.equals(update,Constants.KEY_FALSE)) {
3                     throw new AuthServiceException(ErrorCode.RESOURCE_DUPLICATE_KEY, new Object[]{resource.getFdResName()}, e);
4                 }else if (StringUtils.equals(update,Constants.KEY_TRUE)){
5                     resourceDao.update(resource);
6                 }
7                 throw new AuthServiceException(ErrorCode.RESOURCE_DUPLICATE_KEY, new Object[]{resource.getFdResName()}, e);
8              }

但是在插入重复数据时,报了如下错误:

1 Caused by: org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block

原因是这个类ServiceResource上标记了@Transactional(在没有事务管理的场景下,是可以像上面那样做的):

           
原文地址:https://www.cnblogs.com/lyh421/p/7131169.html