首页  

spring-session-jdbc使用数据库存储共享session     所属分类 springboot 浏览量 1044
spring-session 
session信息保存
本地缓存 文件 redis  数据库

集群共享session , 保存到 redis 或  数据库 中

https://github.com/spring-projects/spring-session

spring-session-jdbc 
使用 数据库 存储 session 信息 

支持多种数据库 
以h2为例
/org/springframework/session/jdbc/schema-h2.sql
两张表
SPRING_SESSION  存储 sessionid
SPRING_SESSION_ATTRIBUTES   存储 session 属性信息

添加依赖
org.springframework.session:spring-session-jdbc

SessionController
session 属性获取 更新 清理

	@RequestMapping("/get")
	public Map getSessionInfo(HttpSession session){
		Map map = new HashMap();		
		map.put("class", session.getClass().toString());	
		String id = session.getId();
		map.put("id", id);
		map.put("createTime", session.getCreationTime());
		map.put("maxInactiveInterval", session.getMaxInactiveInterval());
		map.put("lastAccessedTime", session.getLastAccessedTime());
		map.put("sessionValue", session.getAttribute(NAME));

		return map;
	}
	
Object newValue = LocalDateTime.now().toString();
session.setAttribute(NAME, newValue);
		
session.invalidate();


配置信息

spring.session.store-type=JDBC
# h2 database config
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=


开启 h2-console 控制台

	@Bean
    ServletRegistrationBean h2servletRegistration(){
        ServletRegistrationBean registrationBean = new ServletRegistrationBean( new WebServlet());
        registrationBean.addUrlMappings("/h2-console/*");
        return registrationBean;
    }
    
    
http://127.0.0.1:8090/h2-console/
查询 h2 里数据

完整代码
https://gitee.com/dyyx/springboothello/blob/master/src/main/java/demo/controller/SessionController.java

上一篇     下一篇
java泛型

Linux Load 查看及计算

数据仓库术语

shell里的浮点数运算

linux shell 获取进程相关信息

Linux Shell总结