Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Object Relational Mapping
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Paul Clapham
Ron McLeod
Jeanne Boyarsky
Tim Cooke
Sheriffs:
Liutauras Vilda
paul wheaton
Henry Wong
Saloon Keepers:
Tim Moores
Tim Holloway
Stephan van Hulst
Carey Brown
Frits Walraven
Bartenders:
Piet Souris
Himai Minh
Forum:
Object Relational Mapping
Session is closed
miguel lisboa
Ranch Hand
Posts: 1282
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
i'm quite amazed, because it never happened to me before, and cant figure out what's going on
i've this class:
public class PersistenciaRetencao { private Session s; private Transaction t; private List lista; private String QUERY = "select r.factor from RetencaoNaFonte as r " + "WHERE r.data <= ? " + "order by r.data desc"; public BigDecimal factor(Date data) throws HibernateException { s = HibernateUtil.currentSession(); /* HibernateUtil.closeSession(); s = HibernateUtil.currentSession();*/ t = s.beginTransaction();// error msg: Session is closed lista = s.createQuery(QUERY).setDate(0, data).setFirstResult(0).list(); t.commit(); HibernateUtil.closeSession(); return (BigDecimal)lista.get(0); } }
as you can see, if i run it like it is, i get that error msg; if i uncomment, then everything runs ok
what's going on?
TiA
java amateur
Pierre Sugar
Ranch Hand
Posts: 62
I like...
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
As I understand it runs if you first close the session and then ask for the current session. Right? To see what is going on
you should
make the HibernateUtil class available, I didn't find this in the Hibernate API.
Pierre
miguel lisboa
Ranch Hand
Posts: 1282
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
i use the same code (t = s.beginTransaction()) lots of times in my code and allways works fine... except with this method...
this class is from Hibernate authors:
public class HibernateUtil { private static Log log = LogFactory.getLog(HibernateUtil.class); private static final SessionFactory sessionFactory; static { try { sessionFactory = new Configuration(). configure("/hibernate.cfg.xml").buildSessionFactory(); } catch (HibernateException e) { log.error("Initial SessionFactory creation failed.", e); throw new RuntimeException("Problema de Configura��o: " + e.getMessage(), e); } } public static final ThreadLocal<Session> session = new ThreadLocal<Session>(); public static Session currentSession() throws HibernateException { Session s = (Session) session.get(); // Open a new Session, if this Thread has none yet if (s == null) { s = sessionFactory.openSession(); session.set(s); } return s; } public static void closeSession() throws HibernateException { Session s = (Session) session.get(); session.set(null); if (s != null) s.close(); } }
EDIT:
now that i look at it, i remember i adapted it to java5
[ September 04, 2005: Message edited by: miguel lisboa ]
java amateur
miguel lisboa
Ranch Hand
Posts: 1282
posted 16 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
i sorted it out!
i used that method in a
test
; in this test, before i call it i inserted something into database, and, at the end, instead of closing session like:
HibernateUtil.closeSession()
i used (wrongly):
s.closeSession()
that's it
java amateur
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Do not carry my list on page XHTML
Combo box building
Need help with Hibernate Query language - timestamp field
about inner join and inner join fetch again
hibernate session: to close or not to close
More...