|
44 | 44 | import org.springframework.jdbc.core.JdbcOperations;
|
45 | 45 | import org.springframework.jdbc.core.ResultSetExtractor;
|
46 | 46 | import org.springframework.jdbc.support.lob.DefaultLobHandler;
|
| 47 | +import org.springframework.jdbc.support.lob.LobCreator; |
47 | 48 | import org.springframework.jdbc.support.lob.LobHandler;
|
48 | 49 | import org.springframework.session.DelegatingIndexResolver;
|
49 | 50 | import org.springframework.session.FindByIndexNameSessionRepository;
|
@@ -460,63 +461,65 @@ public Map<String, JdbcSession> findByIndexNameAndIndexValue(String indexName, f
|
460 | 461 |
|
461 | 462 | private void insertSessionAttributes(JdbcSession session, List<String> attributeNames) {
|
462 | 463 | Assert.notEmpty(attributeNames, "attributeNames must not be null or empty");
|
463 |
| - if (attributeNames.size() > 1) { |
464 |
| - this.jdbcOperations.batchUpdate(this.createSessionAttributeQuery, new BatchPreparedStatementSetter() { |
| 464 | + try (LobCreator lobCreator = lobHandler.getLobCreator()) { |
| 465 | + if (attributeNames.size() > 1) { |
| 466 | + this.jdbcOperations.batchUpdate(this.createSessionAttributeQuery, new BatchPreparedStatementSetter() { |
| 467 | + |
| 468 | + @Override |
| 469 | + public void setValues(PreparedStatement ps, int i) throws SQLException { |
| 470 | + String attributeName = attributeNames.get(i); |
| 471 | + ps.setString(1, attributeName); |
| 472 | + lobCreator.setBlobAsBytes(ps, 2, |
| 473 | + serialize(session.getAttribute(attributeName))); |
| 474 | + ps.setString(3, session.getId()); |
| 475 | + } |
465 | 476 |
|
466 |
| - @Override |
467 |
| - public void setValues(PreparedStatement ps, int i) throws SQLException { |
468 |
| - String attributeName = attributeNames.get(i); |
| 477 | + @Override |
| 478 | + public int getBatchSize() { |
| 479 | + return attributeNames.size(); |
| 480 | + } |
| 481 | + |
| 482 | + }); |
| 483 | + } else { |
| 484 | + this.jdbcOperations.update(this.createSessionAttributeQuery, (ps) -> { |
| 485 | + String attributeName = attributeNames.get(0); |
469 | 486 | ps.setString(1, attributeName);
|
470 |
| - getLobHandler().getLobCreator().setBlobAsBytes(ps, 2, |
471 |
| - serialize(session.getAttribute(attributeName))); |
| 487 | + lobCreator.setBlobAsBytes(ps, 2, serialize(session.getAttribute(attributeName))); |
472 | 488 | ps.setString(3, session.getId());
|
473 |
| - } |
474 |
| - |
475 |
| - @Override |
476 |
| - public int getBatchSize() { |
477 |
| - return attributeNames.size(); |
478 |
| - } |
479 |
| - |
480 |
| - }); |
481 |
| - } |
482 |
| - else { |
483 |
| - this.jdbcOperations.update(this.createSessionAttributeQuery, (ps) -> { |
484 |
| - String attributeName = attributeNames.get(0); |
485 |
| - ps.setString(1, attributeName); |
486 |
| - getLobHandler().getLobCreator().setBlobAsBytes(ps, 2, serialize(session.getAttribute(attributeName))); |
487 |
| - ps.setString(3, session.getId()); |
488 |
| - }); |
| 489 | + }); |
| 490 | + } |
489 | 491 | }
|
490 | 492 | }
|
491 | 493 |
|
492 | 494 | private void updateSessionAttributes(JdbcSession session, List<String> attributeNames) {
|
493 | 495 | Assert.notEmpty(attributeNames, "attributeNames must not be null or empty");
|
494 |
| - if (attributeNames.size() > 1) { |
495 |
| - this.jdbcOperations.batchUpdate(this.updateSessionAttributeQuery, new BatchPreparedStatementSetter() { |
| 496 | + try (LobCreator lobCreator = lobHandler.getLobCreator()) { |
| 497 | + if (attributeNames.size() > 1) { |
| 498 | + this.jdbcOperations.batchUpdate(this.updateSessionAttributeQuery, new BatchPreparedStatementSetter() { |
| 499 | + |
| 500 | + @Override |
| 501 | + public void setValues(PreparedStatement ps, int i) throws SQLException { |
| 502 | + String attributeName = attributeNames.get(i); |
| 503 | + lobCreator.setBlobAsBytes(ps, 1, |
| 504 | + serialize(session.getAttribute(attributeName))); |
| 505 | + ps.setString(2, session.primaryKey); |
| 506 | + ps.setString(3, attributeName); |
| 507 | + } |
496 | 508 |
|
497 |
| - @Override |
498 |
| - public void setValues(PreparedStatement ps, int i) throws SQLException { |
499 |
| - String attributeName = attributeNames.get(i); |
500 |
| - getLobHandler().getLobCreator().setBlobAsBytes(ps, 1, |
501 |
| - serialize(session.getAttribute(attributeName))); |
| 509 | + @Override |
| 510 | + public int getBatchSize() { |
| 511 | + return attributeNames.size(); |
| 512 | + } |
| 513 | + |
| 514 | + }); |
| 515 | + } else { |
| 516 | + this.jdbcOperations.update(this.updateSessionAttributeQuery, (ps) -> { |
| 517 | + String attributeName = attributeNames.get(0); |
| 518 | + lobCreator.setBlobAsBytes(ps, 1, serialize(session.getAttribute(attributeName))); |
502 | 519 | ps.setString(2, session.primaryKey);
|
503 | 520 | ps.setString(3, attributeName);
|
504 |
| - } |
505 |
| - |
506 |
| - @Override |
507 |
| - public int getBatchSize() { |
508 |
| - return attributeNames.size(); |
509 |
| - } |
510 |
| - |
511 |
| - }); |
512 |
| - } |
513 |
| - else { |
514 |
| - this.jdbcOperations.update(this.updateSessionAttributeQuery, (ps) -> { |
515 |
| - String attributeName = attributeNames.get(0); |
516 |
| - getLobHandler().getLobCreator().setBlobAsBytes(ps, 1, serialize(session.getAttribute(attributeName))); |
517 |
| - ps.setString(2, session.primaryKey); |
518 |
| - ps.setString(3, attributeName); |
519 |
| - }); |
| 521 | + }); |
| 522 | + } |
520 | 523 | }
|
521 | 524 | }
|
522 | 525 |
|
|
0 commit comments