Redian新闻
>
自己管理session要每次手动释放么
avatar
自己管理session要每次手动释放么# Java - 爪哇娇娃
f*l
1
我现在租住在一个出租屋的主卧,和两个大学舍友住在一起,两个月前,发生了一
些事情,让我有点恐惧。
首先是窗子,有好几次我们明明将窗子打开了,但是等我们回来的时候,窗子却是
关着的,我们的窗子是那种里面有阀门的,所以绝对不是风刮的,因为阀门是被关好的
。虽然事情有点诡异,但是我们还要在这里待一段时间,所以只能调侃,是小妖(家里
养的一只猫)成精了。
第二件事,是厨房的灯,那次我洗完碗,便将灯关掉进屋了,可是,我的一个舍友
看向厨房时,灯却是亮着的,我只好说是我忘关了,但是,我记得清清楚楚,我是关了
灯才出来的,因为当时我两只手都拿着东西,关灯的姿势有些奇怪,所以我记得很清楚。
对了,还有一件事,是最近几天发生的,我在厕所洗手的时候,洗澡用的喷头下的
管子莫名其妙的自己动了一下,吓了我一跳,管子并没有被扭曲,而是垂直挂在那里,
就这样莫名奇妙的动了,幸好自己心理承受能力比较强,要不然非要吓出心脏病不可。
还有两个月就可以搬走了,也就不去管了,只希望能一切顺利吧!
avatar
j*b
2
原名:红颜知己 [小说]
作者:劳柯 [平静幸福]

一阵电话铃声把熟睡的丁乙惊醒,他拿起电话,也没有看来电显示就说:“我的任同学
,又发生什么事情了”电话里果然传出要任佳的声音:“你怎么知道是我啊?你在干什
么啊?”“听呼吸听出来的,我在睡觉。”丁乙说着,打开了床头灯,发现已经一点半
了。“除了你,还有谁敢这么晚给我打电话,先给你说好,今天不能和你长聊,明天早
上有个会议,我一定要去。”“告诉你一个好消息,今天我把驾照考出来了。”任佳高
兴地说。“这是第三次了吧!”“你总是打击我,前两次我没有预备,你知道这一次我
得了多少分吗?” “不知道,应该是满分吧。”“那到不是,九十五分。”“祝贺你
一把,我要睡了。”“别急,我还有点事情要给你说.”
“好吧,你说吧,什么事情?”丁乙无奈地说。
“这个星期六我想去杜邦花园,我开车,当然你要坐我旁边。”任佳说。“这倒是个好
主意,我也早就想去,不过对你开车我还是有点不放心。”丁乙听说要出去玩,一下子
来了精神,接着说“还是我来开车吧!” “我开车是为了练车,你以为什么啊?”“
那好吧,你开车就你开车,叫上杰克,我们一起去。”丁乙说。“不叫他,他
avatar
y*m
3
hiberate的管理在lazy模式等下弄起来挺麻烦,自己管理倒是很方便,但可能存在连接
没有释放的隐患。。。。
import java.io.Serializable;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.springframework.stereotype.Component;
//@Component("hibernateSessionUtil")
public class HibernateSessionUtil implements Serializable {
//Create a thread local variable tLocalsess, for operating session
public static final ThreadLocal tLocalsess = new ThreadLocal();
//Create a thread local variable tLocaltx, used to operate services
public static final ThreadLocal tLocaltx = new ThreadLocal();
//get session
public static Session currentSession(){
//TLocalsess variables from the thread in the current session to obtain
Session session = (Session) tLocalsess.get();
//Determine whether the session is empty, if empty, will create a session
, and pay variable tLocalsess thread
try{
if (session == null){
session = openSession();
tLocalsess.set(session);
}
}catch (HibernateException e){
e.printStackTrace();
}
return session;
}
//close current session
public static void closeSession(){
//TLocalsess variables from the thread in the current session to obtain
Session session = (Session) tLocalsess.get();
//set current tLocalsess as null
tLocalsess.set(null);
try{
//close session
if (session != null && session.isOpen()){
session.close();
}
}catch (HibernateException e){
e.printStackTrace();
}
}
//start transaction
public static void beginTransaction(){
//Variables obtained from the thread of things tLocaltx Transaction
Management Object
Transaction tx = (Transaction) tLocaltx.get();
try{
//If it is empty from the session to create a tx
if (tx == null){
tx = currentSession().beginTransaction();
tLocaltx.set(tx);
}
}catch (HibernateException e){
e.printStackTrace();
}
}
//commit transaction
public static void commitTransaction(){
//get the transaction
Transaction tx = (Transaction) tLocaltx.get();
try{
//if not null, then commit
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack())
tx.commit();
//close current session
closeSession();
tLocaltx.set(null);
}catch (HibernateException e){
e.printStackTrace();
}
}
//rollback the transaction
public static void rollbackTransaction(){
//get tx transaction
Transaction tx = (Transaction) tLocaltx.get();
try{
//Clear the variable
tLocaltx.set(null);
if (tx != null && !tx.wasCommitted() && !tx.wasRolledBack()){
//rollback the transaction
tx.rollback();
}
}catch (HibernateException e){
e.printStackTrace();
}
}
//private
//get session
private static Session openSession() throws HibernateException{
return getSessionFactory().openSession();
}
//get sessionFactory
private static SessionFactory getSessionFactory() throws
HibernateException{
return HibernateSessionFactory.getSessionFactory();
}
}
avatar
t*e
4
google "open session in view"
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。