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 61492c4 commit cbd9699
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 @@ -123,6 +137,16 @@ private String getTableName() {
return this.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 cbd9699

Please sign in to comment.