-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: hibernate session
PostPosted: Mon Feb 21, 2005 6:13 pm 
Newbie

Joined: Mon Feb 21, 2005 6:13 pm
Posts: 5
Location: baltimore
Ok, I know I am just doing something stupid here, but could someone give me a heads up on what that is. I have objects that must be lazily loaded that at some places are 4-5 levels deep. These objects are needed in various places of my application. To prevent my sessions from being closed, I have the following method:

/**
*
* @throws ReportTransactionException
*/
public int startTransaction() throws ReportTransactionException {
if(transactionIsOpen) {
log.warn("transaction was already open");
return -1;
}

try {
sessionFactory = (SessionFactory) appContext
.getBean("sessionFactory");
Session s = sessionFactory.openSession();

TransactionSynchronizationManager.bindResource(sessionFactory,
new SessionHolder(s));
transactionIsOpen = true;
log.info("starting transaction");
return 1;
} catch (HibernateException he) {
log.error("cant start transaction");
transactionIsOpen = false;
throw new ReportTransactionException();
}

}


and then I have....


/**
*
* @throws ReportTransactionException
*/
public void endTransaction() throws ReportTransactionException {
if(!transactionIsOpen) {
log.warn("transaction was already closed");
return;
}
try {
SessionHolder holder = (SessionHolder) TransactionSynchronizationManager
.getResource(sessionFactory);
if(holder == null) {
throw new ReportTransactionException();
}
Session s = holder.getSession();
s.flush();
TransactionSynchronizationManager.unbindResource(sessionFactory);
SessionFactoryUtils.closeSessionIfNecessary(s, sessionFactory);
transactionIsOpen = false;
log.info("closing transaction");
} catch (HibernateException he) {
log.error("cant stop transaction");
throw new ReportTransactionException();
}
}

to close the session...


I open the session in my constructor, and close the session just before the application terminates. However, I get the following error message:

[wct] ERROR [Thread-8] LazyInitializationException.<init>(25) | Failed to lazily initialize a collection - no session or session was closed
net.sf.hibernate.LazyInitializationException: Failed to lazily initialize a collection - no session or session was closed
at net.sf.hibernate.collection.PersistentCollection.initialize(PersistentCollection.java:209)
at net.sf.hibernate.collection.PersistentCollection.read(PersistentCollection.java:71)
at net.sf.hibernate.collection.Bag.size(Bag.java:232)
at com.bd.profit.report.data.reportTable.ReportRow.initializeStatisticalData(ReportRow.java:268)
at com.bd.profit.report.data.reportTable.ReportRow.<init>(ReportRow.java:135)
at com.bd.profit.report.data.modelBuilders.ReportTableModelBuilder.buildTableModel(ReportTableModelBuilder.java:50)
at com.bd.profit.report.managers.ModelManager.buildReportTableModel(ModelManager.java:133)
at com.bd.profit.report.gui.panels.Document.createTable(Document.java:145)
at com.bd.profit.report.gui.panels.Document.createNewTable(Document.java:128)
at com.bd.profit.report.gui.panels.Document.<init>(Document.java:8Cool
at com.bd.profit.report.ReportGenerator$TabCreator.construct(ReportGenerator.java:330)
at org.jdesktop.swing.utils.SwingWorker$2.run(SwingWorker.java:116)
at java.lang.Thread.run(Thread.java:595)


Why is this occuring? Is it because I have multiple threads accessing the same session?

Thx in advance,

Nick


Top
 Profile  
 
 Post subject: Re: hibernate session
PostPosted: Mon Feb 21, 2005 7:31 pm 
Regular
Regular

Joined: Thu Dec 18, 2003 2:14 am
Posts: 103
Location: Brooklyn, NY
nchristy wrote:
Why is this occuring? Is it because I have multiple threads accessing the same session?

I would guess so. Do you really need multiple threads accessing the _same_ session? I can't think of a use case for it. What is the requirement?

Usually you get the data, and return, perhaps display or modify, and then flush and close. Each command goes to the database on its own, then closes the connection.

Please show us some more of your code, where the command is actually occurring. And how your session handling code wraps the command.


Top
 Profile  
 
 Post subject: more code
PostPosted: Tue Feb 22, 2005 9:44 am 
Newbie

Joined: Mon Feb 21, 2005 6:13 pm
Posts: 5
Location: baltimore
The reason I have multiple threads is because it is a swing app and I am spawning new threads to take care of long data retrievals so that they do not occur on the same thread as the gui. For example,

public class ReportGenerator {
.
.
.
.
/**
* @param event
*/
public void showReport(ShowReportEvent event) {
Report report = event.getReport();
SwingWorker createTab;
navigationPanel.expandHistory(report.getReportTyp());
createTab = new TabCreator(report.getReportName());
createTab.start();
}

.
.
.
.

private final class TabCreator extends SwingWorker {
private Report report;
private String displayName;

private TabCreator(Report report) {
super();
this.report = report
}

/**
* @return Object
*/
public Object construct() {
log.warn("SESSION IS OPEN (T/F)" + application.isSessionOpen());
Document document = new Document(report);
documents.add(document);
return document;
}

/**
*
*/
public void finished() {
Document document = (Document) get();
if(report != null) {
displayNewReport(document, report.getReportName());
}
}
}


Each Report consists of 1-2k products. When a document is created, the indidual Products within a Report are accessed. These Products are lazily loaded. When the document attempts to load the product from the database after retrieving it from the Report the application throws the Lazily Loaded No active session exception.

Does this help?

Thx,

Nick


Top
 Profile  
 
 Post subject: sorry forgot to add this
PostPosted: Tue Feb 22, 2005 9:47 am 
Newbie

Joined: Mon Feb 21, 2005 6:13 pm
Posts: 5
Location: baltimore
The startTransaction() method is called within the class constructor as such:

public ReportGenerator() {
super();
loadLookAndFeel();
application.startSession();
Runtime.getRuntime().addShutdownHook(new MyShutdownHook());

documents = new ArrayList<Document>();
SwingWorker launcher = new Launch();
splashScreen = new ReportSplashScreen();
splashScreen.setVisible(true);
if (security.hasRole(Role.CanAccessReportGenerator)) {
launcher.start();
} else {
this.dispose();
}

}

And the EndTransaction is called within the ShutdownHook as such:


private class MyShutdownHook extends Thread {
public void run() {
shutdown();
}
}

private void shutdown() {
application.endSession();
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.