Commit a361ee9403fa289bcd1672ab8ada172bd756d260
1 parent
bc9760ff
Exists in
master
and in
1 other branch
修复keyholder在并发场景下,查询出错的问题
Showing
4 changed files
with
7 additions
and
14 deletions
Show diff stats
build.gradle
src/main/java/com/taover/repository/CustomJdbcTemplate.java
| ... | ... | @@ -4,7 +4,6 @@ import java.io.Serializable; |
| 4 | 4 | import java.lang.reflect.Field; |
| 5 | 5 | import java.lang.reflect.ParameterizedType; |
| 6 | 6 | import java.math.BigDecimal; |
| 7 | -import java.math.BigInteger; | |
| 8 | 7 | import java.sql.Connection; |
| 9 | 8 | import java.sql.PreparedStatement; |
| 10 | 9 | import java.sql.SQLException; |
| ... | ... | @@ -52,7 +51,6 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 52 | 51 | private String tableName; |
| 53 | 52 | private Class<T> tClassInfo; |
| 54 | 53 | private CustomJdbcTemplateRowMapper customJdbcTemplateRowMapper; |
| 55 | - private KeyHolder keyHolder; | |
| 56 | 54 | |
| 57 | 55 | public CustomJdbcTemplateRowMapper getCustomJdbcTemplateRowMapper(){ |
| 58 | 56 | return this.customJdbcTemplateRowMapper; |
| ... | ... | @@ -122,9 +120,6 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 122 | 120 | |
| 123 | 121 | //创建rowmapper |
| 124 | 122 | this.customJdbcTemplateRowMapper = new CustomJdbcTemplateRowMapper(this.tClassInfo, this.tableToBeanField); |
| 125 | - | |
| 126 | - //主键ID保存 | |
| 127 | - this.keyHolder = new GeneratedKeyHolder(); | |
| 128 | 123 | } |
| 129 | 124 | |
| 130 | 125 | /** |
| ... | ... | @@ -464,6 +459,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 464 | 459 | |
| 465 | 460 | //执行SQL |
| 466 | 461 | String exeSql = sqlInsertPart.substring(0, sqlInsertPart.length()-1)+sqlColumnPart.substring(0, sqlColumnPart.length()-1)+")"; |
| 462 | + KeyHolder keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(1)); | |
| 467 | 463 | jdbcTemplateWrite.update(new PreparedStatementCreator() { |
| 468 | 464 | @Override |
| 469 | 465 | public PreparedStatement createPreparedStatement(Connection con) throws SQLException { |
| ... | ... | @@ -512,6 +508,7 @@ public class CustomJdbcTemplate<T, ID extends Serializable> { |
| 512 | 508 | exeSql.setCharAt(exeSql.length()-1, ';'); |
| 513 | 509 | |
| 514 | 510 | //调用更新接口 |
| 511 | + KeyHolder keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(entityList.size())); | |
| 515 | 512 | jdbcTemplateWrite.update(new PreparedStatementCreator() { |
| 516 | 513 | @Override |
| 517 | 514 | public PreparedStatement createPreparedStatement(Connection con) throws SQLException { | ... | ... |
src/main/java/com/taover/repository/CustomJdbcTemplateBroadcast.java
| ... | ... | @@ -54,7 +54,6 @@ public class CustomJdbcTemplateBroadcast<T, ID extends Serializable> implements |
| 54 | 54 | private String _tableName; |
| 55 | 55 | private Class<T> _tClassInfo; |
| 56 | 56 | private CustomJdbcTemplateRowMapper<T> _customJdbcTemplateRowMapper; |
| 57 | - private KeyHolder _keyHolder; | |
| 58 | 57 | |
| 59 | 58 | public CustomJdbcTemplateRowMapper<T> getCustomJdbcTemplateRowMapper(){ |
| 60 | 59 | return this._customJdbcTemplateRowMapper; |
| ... | ... | @@ -118,9 +117,6 @@ public class CustomJdbcTemplateBroadcast<T, ID extends Serializable> implements |
| 118 | 117 | |
| 119 | 118 | //创建rowmapper |
| 120 | 119 | this._customJdbcTemplateRowMapper = new CustomJdbcTemplateRowMapper<T>(this._tClassInfo, this._tableToBeanField); |
| 121 | - | |
| 122 | - //主键ID保存 | |
| 123 | - this._keyHolder = new GeneratedKeyHolder(); | |
| 124 | 120 | } |
| 125 | 121 | |
| 126 | 122 | /** |
| ... | ... | @@ -397,6 +393,7 @@ public class CustomJdbcTemplateBroadcast<T, ID extends Serializable> implements |
| 397 | 393 | |
| 398 | 394 | //执行SQL |
| 399 | 395 | String exeSql = sqlInsertPart.substring(0, sqlInsertPart.length()-1)+sqlColumnPart.substring(0, sqlColumnPart.length()-1)+")"; |
| 396 | + KeyHolder _keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(1)); | |
| 400 | 397 | _jdbcTemplateBroadcast.update(new PreparedStatementCreator() { |
| 401 | 398 | @Override |
| 402 | 399 | public PreparedStatement createPreparedStatement(Connection con) throws SQLException { |
| ... | ... | @@ -447,6 +444,7 @@ public class CustomJdbcTemplateBroadcast<T, ID extends Serializable> implements |
| 447 | 444 | exeSql.setCharAt(exeSql.length()-1, ';'); |
| 448 | 445 | |
| 449 | 446 | //调用更新接口 |
| 447 | + KeyHolder _keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(entityList.size())); | |
| 450 | 448 | _jdbcTemplateBroadcast.update(new PreparedStatementCreator() { |
| 451 | 449 | @Override |
| 452 | 450 | public PreparedStatement createPreparedStatement(Connection con) throws SQLException { | ... | ... |
src/main/java/com/taover/repository/CustomJdbcTemplateWrapperTenant.java
| ... | ... | @@ -54,7 +54,6 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
| 54 | 54 | private String _tableName; |
| 55 | 55 | private Class<T> _tClassInfo; |
| 56 | 56 | private CustomJdbcTemplateRowMapper<T> _customJdbcTemplateRowMapper; |
| 57 | - private KeyHolder _keyHolder; | |
| 58 | 57 | |
| 59 | 58 | public CustomJdbcTemplateRowMapper<T> getCustomJdbcTemplateRowMapper(){ |
| 60 | 59 | return this._customJdbcTemplateRowMapper; |
| ... | ... | @@ -118,9 +117,6 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
| 118 | 117 | |
| 119 | 118 | //创建rowmapper |
| 120 | 119 | this._customJdbcTemplateRowMapper = new CustomJdbcTemplateRowMapper<T>(this._tClassInfo, this._tableToBeanField); |
| 121 | - | |
| 122 | - //主键ID保存 | |
| 123 | - this._keyHolder = new GeneratedKeyHolder(); | |
| 124 | 120 | } |
| 125 | 121 | |
| 126 | 122 | /** |
| ... | ... | @@ -399,6 +395,7 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
| 399 | 395 | |
| 400 | 396 | //执行SQL |
| 401 | 397 | String exeSql = sqlInsertPart.substring(0, sqlInsertPart.length()-1)+sqlColumnPart.substring(0, sqlColumnPart.length()-1)+")"; |
| 398 | + KeyHolder _keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(1)); | |
| 402 | 399 | _jdbcTemplateWrapperTenant.update(new PreparedStatementCreator() { |
| 403 | 400 | @Override |
| 404 | 401 | public PreparedStatement createPreparedStatement(Connection con) throws SQLException { |
| ... | ... | @@ -449,6 +446,7 @@ public class CustomJdbcTemplateWrapperTenant<T, ID extends Serializable> impleme |
| 449 | 446 | exeSql.setCharAt(exeSql.length()-1, ';'); |
| 450 | 447 | |
| 451 | 448 | //调用更新接口 |
| 449 | + KeyHolder _keyHolder = new GeneratedKeyHolder(new ArrayList<Map<String,Object>>(entityList.size())); | |
| 452 | 450 | _jdbcTemplateWrapperTenant.update(new PreparedStatementCreator() { |
| 453 | 451 | @Override |
| 454 | 452 | public PreparedStatement createPreparedStatement(Connection con) throws SQLException { | ... | ... |