Skip to content

Commit

Permalink
JDBC uses Bean ClassLoader
Browse files Browse the repository at this point in the history
This commit addresses the issue with deserializing JDBC sessions in Spring Boot
applications that use DevTools. Previously, such configuration would cause
`ClassCastException` when deserializing JDBC sessions due to app class loader
being used instead of restart class loader.

Fixes gh-610
  • Loading branch information
vpavic authored and Rob Winch committed Sep 7, 2016
1 parent b79dc79 commit 62b5efe
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@

import javax.sql.DataSource;

import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportAware;
import org.springframework.core.annotation.AnnotationAttributes;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.core.type.AnnotationMetadata;
import org.springframework.jdbc.core.JdbcOperations;
import org.springframework.jdbc.core.JdbcTemplate;
Expand All @@ -52,7 +56,7 @@
@Configuration
@EnableScheduling
public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration
implements ImportAware {
implements BeanClassLoaderAware, ImportAware {

private String tableName = "";

Expand All @@ -66,6 +70,8 @@ public class JdbcHttpSessionConfiguration extends SpringHttpSessionConfiguration

private ConversionService springSessionConversionService;

private ClassLoader classLoader;

@Bean
public JdbcTemplate springSessionJdbcOperations(DataSource dataSource) {
return new JdbcTemplate(dataSource);
Expand All @@ -92,6 +98,14 @@ public JdbcOperationsSessionRepository sessionRepository(
else if (this.conversionService != null) {
sessionRepository.setConversionService(this.conversionService);
}
else if (this.classLoader != null) {
GenericConversionService conversionService = new GenericConversionService();
conversionService.addConverter(Object.class, byte[].class,
new SerializingConverter());
conversionService.addConverter(byte[].class, Object.class,
new DeserializingConverter(this.classLoader));
sessionRepository.setConversionService(conversionService);
}
return sessionRepository;
}

Expand Down Expand Up @@ -122,6 +136,16 @@ private String getTableName() {
return System.getProperty("spring.session.jdbc.tableName", "");
}

public void setBeanClassLoader(ClassLoader classLoader) {
try {
DeserializingConverter.class.getConstructor(ClassLoader.class);
}
catch (NoSuchMethodException e) {
return;
}
this.classLoader = classLoader;
}

public void setImportMetadata(AnnotationMetadata importMetadata) {
Map<String, Object> enableAttrMap = importMetadata
.getAnnotationAttributes(EnableJdbcHttpSession.class.getName());
Expand Down

0 comments on commit 62b5efe

Please sign in to comment.