1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
package dao.tools;
import java.util.Timer;
import java.util.TimerTask;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
/**
* Utility Class for Hibernate Connection
*
*/
public class CIRHibernateUtil
{
public static final ThreadLocal session = new ThreadLocal();
public static final Configuration config = new Configuration().configure();
private static final SessionFactory factory = config.buildSessionFactory();
// 3mn
private static long timeout = 180000;
/**
* Return current Hibernate Session for the user
* @return
* @throws HibernateException
*/
public static Session currentSession() throws HibernateException
{
return CIRHibernateUtil.currentSession(CIRHibernateUtil.timeout);
}
/**
* Return current Hibernate Session for the user
* @return
* @throws HibernateException
*/
public static Session currentSession(long timeout) throws HibernateException
{
Session s = null;
// Get session and timer bean
SessionTimerBean stb = (SessionTimerBean) session.get();
if(stb != null)
{
s = stb.getSession();
Timer timer = stb.getTimer();
SessionTimerTask stt = stb.getStt();
stt.cancel();
// If session is closed
if ((s == null)||(!s.isOpen()))
{
s = factory.openSession();
timer.cancel();
timer = new Timer();
stt = new CIRHibernateUtil().new SessionTimerTask();
// Create a timeout for session
timer.schedule(stt, timeout);
session.set(stb);
}
else
{
// Reset timeout for session
timer.cancel();
timer = new Timer();
stt = new CIRHibernateUtil().new SessionTimerTask();
// Create a timeout for session
timer.schedule(stt, timeout);
}
// Set Timer
stb.setTimer(timer);
}
else
{
s = factory.openSession();
Timer timer = new Timer();
SessionTimerTask stt = new CIRHibernateUtil().new SessionTimerTask();
timer.schedule(stt, timeout);
stb = new CIRHibernateUtil().new SessionTimerBean(s, timer, stt);
stt.setStb(stb);
session.set(stb);
}
return s;
}
/**
* Return a new Hibernate Session
* @return
* @throws HibernateException
*/
public static Session newSession() throws HibernateException
{
Session s = factory.openSession();
return s;
}
/**
* Close session of current user
*
*/
public static void closeSession()
{
SessionTimerBean stb = (SessionTimerBean) session.get();
if(stb != null)
{
Session s = stb.getSession();
Timer timer = stb.getTimer();
SessionTimerTask stt = stb.getStt();
if (s != null)
{
if(s.isOpen())
{
s.close();
timer.cancel();
stt.cancel();
}
}
CIRHibernateUtil.session.set(null);
}
}
/**
* Close session in parameter
* @param session
*/
public static void closeSession(Session session)
{
session.close();
}
/**
* Timeout Thread Class
*
*/
public class SessionTimerTask extends TimerTask
{
// Attributes
private SessionTimerBean stb;
/**
* Constructor
* @param stb : <code>SessionTimerBean</code>
*/
public SessionTimerTask()
{
super();
}
/**
* Method runned when time is up
* Close session
*/
public void run()
{
try
{
// If time is up, stop the process
if(this.stb != null)
{
Session session = this.stb.getSession();
Timer timer = this.stb.getTimer();
if(session != null)
{
if(session.isOpen())
{
System.out.println("close de service");
session.close();
timer.cancel();
this.cancel();
}
}
}
}
catch(Exception e)
{
e.printStackTrace();
}
}
/**
* @return the stb
*/
public SessionTimerBean getStb() {
return stb;
}
/**
* @param stb the stb to set
*/
public void setStb(SessionTimerBean stb) {
this.stb = stb;
}
}
/**
* Bean class used to store a Session and the Timeout thread
*
*/
public class SessionTimerBean
{
// Attributes
private Session session;
private Timer timer;
private SessionTimerTask stt;
/**
* Constructor
* @param session
* @param timer
* @param stt
*/
public SessionTimerBean(Session session, Timer timer, SessionTimerTask stt)
{
super();
this.session = session;
this.timer = timer;
this.stt = stt;
}
/**
* @return the session
*/
public Session getSession() {
return session;
}
/**
* @return the stt
*/
public SessionTimerTask getStt() {
return stt;
}
/**
* @return the timer
*/
public Timer getTimer() {
return timer;
}
/**
* @param session the session to set
*/
public void setSession(Session session) {
this.session = session;
}
/**
* @param stt the stt to set
*/
public void setStt(SessionTimerTask stt) {
this.stt = stt;
}
/**
* @param timer the timer to set
*/
public void setTimer(Timer timer) {
this.timer = timer;
}
}
} |
Partager