28
28
import java .util .Map ;
29
29
import java .util .Set ;
30
30
import java .util .UUID ;
31
+ import java .util .function .Supplier ;
31
32
import java .util .stream .Collectors ;
32
33
33
34
import org .apache .commons .logging .Log ;
@@ -530,7 +531,7 @@ private void insertSessionAttributes(JdbcSession session, List<String> attribute
530
531
public void setValues (PreparedStatement ps , int i ) throws SQLException {
531
532
String attributeName = attributeNames .get (i );
532
533
ps .setString (1 , attributeName );
533
- serialize (ps , 2 , session .getAttribute (attributeName ));
534
+ bytesToLob (ps , 2 , serialize ( session .getAttribute (attributeName ) ));
534
535
ps .setString (3 , session .getId ());
535
536
}
536
537
@@ -545,7 +546,7 @@ public int getBatchSize() {
545
546
this .jdbcOperations .update (this .createSessionAttributeQuery , (ps ) -> {
546
547
String attributeName = attributeNames .get (0 );
547
548
ps .setString (1 , attributeName );
548
- serialize (ps , 2 , session .getAttribute (attributeName ));
549
+ bytesToLob (ps , 2 , serialize ( session .getAttribute (attributeName ) ));
549
550
ps .setString (3 , session .getId ());
550
551
});
551
552
}
@@ -559,7 +560,7 @@ private void updateSessionAttributes(JdbcSession session, List<String> attribute
559
560
@ Override
560
561
public void setValues (PreparedStatement ps , int i ) throws SQLException {
561
562
String attributeName = attributeNames .get (i );
562
- serialize (ps , 1 , session .getAttribute (attributeName ));
563
+ bytesToLob (ps , 1 , serialize ( session .getAttribute (attributeName ) ));
563
564
ps .setString (2 , session .primaryKey );
564
565
ps .setString (3 , attributeName );
565
566
}
@@ -574,7 +575,7 @@ public int getBatchSize() {
574
575
else {
575
576
this .jdbcOperations .update (this .updateSessionAttributeQuery , (ps ) -> {
576
577
String attributeName = attributeNames .get (0 );
577
- serialize (ps , 1 , session .getAttribute (attributeName ));
578
+ bytesToLob (ps , 1 , serialize ( session .getAttribute (attributeName ) ));
578
579
ps .setString (2 , session .primaryKey );
579
580
ps .setString (3 , attributeName );
580
581
});
@@ -657,18 +658,26 @@ private void prepareQueries() {
657
658
getQuery (DELETE_SESSIONS_BY_EXPIRY_TIME_QUERY );
658
659
}
659
660
660
- private void serialize (PreparedStatement ps , int paramIndex , Object attributeValue )
661
+ private void bytesToLob (PreparedStatement ps , int paramIndex , byte [] bytes )
661
662
throws SQLException {
662
663
this .lobHandler .getLobCreator ().setBlobAsBytes (ps , paramIndex ,
663
- (byte []) this .conversionService .convert (attributeValue ,
664
+ bytes );
665
+ }
666
+
667
+ private byte [] serialize (Object attributeValue ) {
668
+ return (byte []) this .conversionService .convert (attributeValue ,
664
669
TypeDescriptor .valueOf (Object .class ),
665
- TypeDescriptor .valueOf (byte [].class ))) ;
670
+ TypeDescriptor .valueOf (byte [].class ));
666
671
}
667
672
668
- private Object deserialize (ResultSet rs , String columnName )
673
+ private byte [] lobToBytes (ResultSet rs , String columnName )
669
674
throws SQLException {
675
+ return this .lobHandler .getBlobAsBytes (rs , columnName );
676
+ }
677
+
678
+ private Object deserialize (byte [] bytes ) {
670
679
return this .conversionService .convert (
671
- this . lobHandler . getBlobAsBytes ( rs , columnName ) ,
680
+ bytes ,
672
681
TypeDescriptor .valueOf (byte [].class ),
673
682
TypeDescriptor .valueOf (Object .class ));
674
683
}
@@ -679,6 +688,34 @@ private enum DeltaValue {
679
688
680
689
}
681
690
691
+ static final <Z > Supplier <Z > constantSupplier (Z value ) {
692
+ if (value == null ) {
693
+ return null ;
694
+ }
695
+ else {
696
+ return () -> value ;
697
+ }
698
+ }
699
+
700
+ static final <Z > Supplier <Z > lazily (Supplier <Z > supplier ) {
701
+ if (supplier == null ) {
702
+ return null ;
703
+ }
704
+ else {
705
+ return new Supplier <Z >() {
706
+ private Z value ;
707
+
708
+ @ Override
709
+ public Z get () {
710
+ if (this .value == null ) {
711
+ this .value = supplier .get ();
712
+ }
713
+ return this .value ;
714
+ }
715
+ };
716
+ }
717
+ }
718
+
682
719
/**
683
720
* The {@link Session} to use for {@link JdbcOperationsSessionRepository}.
684
721
*
@@ -748,7 +785,13 @@ public String changeSessionId() {
748
785
749
786
@ Override
750
787
public <T > T getAttribute (String attributeName ) {
751
- return this .delegate .getAttribute (attributeName );
788
+ Supplier <T > supplier = this .delegate .getAttribute (attributeName );
789
+ if (supplier == null ) {
790
+ return null ;
791
+ }
792
+ else {
793
+ return supplier .get ();
794
+ }
752
795
}
753
796
754
797
@ Override
@@ -783,7 +826,7 @@ public void setAttribute(String attributeName, Object attributeValue) {
783
826
? oldDeltaValue
784
827
: DeltaValue .UPDATED );
785
828
}
786
- this .delegate .setAttribute (attributeName , attributeValue );
829
+ this .delegate .setAttribute (attributeName , constantSupplier ( attributeValue ) );
787
830
if (PRINCIPAL_NAME_INDEX_NAME .equals (attributeName ) ||
788
831
SPRING_SECURITY_CONTEXT .equals (attributeName )) {
789
832
this .changed = true ;
@@ -875,7 +918,8 @@ public List<JdbcSession> extractData(ResultSet rs) throws SQLException, DataAcce
875
918
}
876
919
String attributeName = rs .getString ("ATTRIBUTE_NAME" );
877
920
if (attributeName != null ) {
878
- session .delegate .setAttribute (attributeName , deserialize (rs , "ATTRIBUTE_BYTES" ));
921
+ byte [] bytes = lobToBytes (rs , "ATTRIBUTE_BYTES" );
922
+ session .delegate .setAttribute (attributeName , lazily (() -> deserialize (bytes )));
879
923
}
880
924
sessions .add (session );
881
925
}
0 commit comments