|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| ThreadLocalSessionProvider.java | 0% | 0% | 0% | 0% |
|
||||||||||||||
| 1 |
/*
|
|
| 2 |
* ThreadLocalSessionProvider.java
|
|
| 3 |
* Copyright 2002-2004 Bill2, Inc.
|
|
| 4 |
*
|
|
| 5 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
| 6 |
* you may not use this file except in compliance with the License.
|
|
| 7 |
* You may obtain a copy of the License at
|
|
| 8 |
*
|
|
| 9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
| 10 |
*
|
|
| 11 |
* Unless required by applicable law or agreed to in writing, software
|
|
| 12 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
| 13 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
| 14 |
* See the License for the specific language governing permissions and
|
|
| 15 |
* limitations under the License.
|
|
| 16 |
*/
|
|
| 17 |
package org.dentaku.services.persistence.hibernate;
|
|
| 18 |
|
|
| 19 |
import net.sf.hibernate.HibernateException;
|
|
| 20 |
import net.sf.hibernate.Session;
|
|
| 21 |
import net.sf.hibernate.SessionFactory;
|
|
| 22 |
import net.sf.hibernate.Transaction;
|
|
| 23 |
import org.codehaus.plexus.logging.LogEnabled;
|
|
| 24 |
import org.codehaus.plexus.logging.Logger;
|
|
| 25 |
import org.codehaus.plexus.logging.console.ConsoleLogger;
|
|
| 26 |
import org.dentaku.services.persistence.PersistenceException;
|
|
| 27 |
|
|
| 28 |
import java.lang.reflect.Method;
|
|
| 29 |
|
|
| 30 |
public class ThreadLocalSessionProvider implements SessionProvider, LogEnabled { |
|
| 31 |
static Object hbnObject = null; |
|
| 32 |
static ThreadLocal session = new ThreadLocal(); |
|
| 33 |
static ThreadLocal transaction = new ThreadLocal(); |
|
| 34 |
SessionFactory factory; |
|
| 35 |
static Logger log = new ConsoleLogger(Logger.LEVEL_DEBUG, ThreadLocalSessionProvider.class.getName()); |
|
| 36 |
|
|
| 37 | 0 |
public Session getSession() throws PersistenceException { |
| 38 | 0 |
return getThreadLocalSession();
|
| 39 |
} |
|
| 40 |
|
|
| 41 | 0 |
public static Session getThreadLocalSession() throws PersistenceException { |
| 42 | 0 |
Session sess = (Session) session.get(); |
| 43 | 0 |
try {
|
| 44 | 0 |
if (sess == null) { |
| 45 | 0 |
sess = getFactory().openSession(); |
| 46 | 0 |
Transaction tr = sess.beginTransaction(); |
| 47 | 0 |
session.set(sess); |
| 48 | 0 |
transaction.set(tr); |
| 49 | 0 |
if (log.isDebugEnabled()) {
|
| 50 | 0 |
log.debug("created session and started new transaction");
|
| 51 |
} |
|
| 52 |
} |
|
| 53 | 0 |
if (!sess.isConnected()) {
|
| 54 | 0 |
sess.reconnect(); |
| 55 |
} |
|
| 56 |
} catch (HibernateException e) {
|
|
| 57 | 0 |
throw new PersistenceException(e); |
| 58 |
} |
|
| 59 | 0 |
return sess;
|
| 60 |
} |
|
| 61 |
|
|
| 62 | 0 |
public void resetSession() throws PersistenceException { |
| 63 | 0 |
session.set(null);
|
| 64 | 0 |
transaction.set(null);
|
| 65 |
} |
|
| 66 |
|
|
| 67 | 0 |
public void releaseSession() throws PersistenceException { |
| 68 | 0 |
releaseSessionImpl(); |
| 69 |
} |
|
| 70 |
|
|
| 71 | 0 |
public void releaseSessionImpl() throws PersistenceException { |
| 72 | 0 |
try {
|
| 73 | 0 |
Transaction tr = (Transaction) transaction.get(); |
| 74 | 0 |
if (tr != null && !tr.wasCommitted() && !tr.wasRolledBack()) { |
| 75 | 0 |
transaction.set(null);
|
| 76 | 0 |
tr.commit(); |
| 77 |
} |
|
| 78 | 0 |
Session session = ((Session)ThreadLocalSessionProvider.session.get()); |
| 79 | 0 |
if (session != null) { |
| 80 | 0 |
session.close(); |
| 81 | 0 |
ThreadLocalSessionProvider.session.set(null);
|
| 82 | 0 |
if (log.isDebugEnabled()) {
|
| 83 | 0 |
log.debug("returned session and closed it");
|
| 84 |
} |
|
| 85 |
} |
|
| 86 |
} catch (HibernateException e) {
|
|
| 87 | 0 |
throw new PersistenceException(e); |
| 88 |
} |
|
| 89 |
} |
|
| 90 |
|
|
| 91 | 0 |
public void rollback() { |
| 92 | 0 |
rollbackImpl(); |
| 93 |
} |
|
| 94 |
|
|
| 95 | 0 |
public static void rollbackImpl() { |
| 96 | 0 |
Transaction tr = (Transaction) transaction.get(); |
| 97 | 0 |
if (tr != null) { |
| 98 | 0 |
try {
|
| 99 | 0 |
tr.rollback(); |
| 100 |
} catch (HibernateException e) {
|
|
| 101 |
} finally {
|
|
| 102 | 0 |
transaction.set(null);
|
| 103 |
} |
|
| 104 |
} |
|
| 105 |
} |
|
| 106 |
|
|
| 107 | 0 |
public void enableLogging(Logger logger) { |
| 108 | 0 |
log = logger; |
| 109 |
} |
|
| 110 |
|
|
| 111 |
/**
|
|
| 112 |
* This method uses reflection to call to avoid a circular reference with the model project
|
|
| 113 |
*
|
|
| 114 |
* @return
|
|
| 115 |
* @throws HibernateException
|
|
| 116 |
*/
|
|
| 117 | 0 |
private static SessionFactory getFactory() throws PersistenceException { |
| 118 | 0 |
try {
|
| 119 | 0 |
Method m = Class.forName("org.dentaku.container.jboss.HibernateUtil").getMethod("getSessionFactory", null); |
| 120 | 0 |
return (SessionFactory) m.invoke(null, null); |
| 121 |
} catch (Exception e) {
|
|
| 122 | 0 |
throw new PersistenceException(e); |
| 123 |
} |
|
| 124 |
} |
|
| 125 |
} |
|
| 126 |
|
|
||||||||||