-->
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: SessionException Session is closed. Blob Mysql
PostPosted: Thu Jan 26, 2006 6:30 pm 
Newbie

Joined: Fri Jul 22, 2005 8:47 am
Posts: 8
Hi,
I need some help with hibernate 3.1.
i am migrate an application from hibernate 2.x to hibernate 3.1.1.
I'm ussing the pattern ThreadLocal to session implementation was used in hibernate 2.x and i haven't problem with this until now.

I get this exception when serialize the object to setting blob database column.

Hibernate version:3.1.1

Mapping documents:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping
>
<class
name="com.mjoy.dcmbase.entities.Sesion"
table="sesion"
>

<id
name="id"
column="id"
type="java.lang.Long"
unsaved-value="null"
>
<generator class="native">
<!--
To add non XDoclet generator parameters, create a file named
hibernate-generator-params-Sesion.xml
containing the additional parameters and place it in your merge dir.
-->
</generator>
</id>

<property
name="datos"
type="java.sql.Blob"
update="true"
insert="true"
access="field"
column="datos"
not-null="false"
unique="false"
/>

<property
name="linea"
type="java.lang.Long"
update="true"
insert="true"
column="linea"
not-null="false"
unique="false"
/>

<property
name="status"
type="java.lang.Integer"
update="true"
insert="true"
column="status"
not-null="false"
unique="false"
/>

<property
name="fecha"
type="java.sql.Timestamp"
update="true"
insert="true"
column="fecha"
not-null="false"
unique="false"
/>

<!--
To add non XDoclet property mappings, create a file named
hibernate-properties-Sesion.xml
containing the additional properties and place it in your merge dir.
-->

</class>

<query name="ObtenerSesionLinea"><![CDATA[
from Sesion as s where s.linea like :linea
]]></query>

</hibernate-mapping>



Code between sessionFactory.openSession() and session.close():

/**
* Este metodo tiene la siguiente funcionalidad:
* Tranform map attributes to blob object
* @param atributos
*/
public void setDatos(Map atributos) {
ByteArrayOutputStream output = null;
ObjectOutputStream objectSerializable = null;

try {
output = new ByteArrayOutputStream();
log.debug("Obtain object input stream to flush blob");
objectSerializable = new ObjectOutputStream(output);
log.debug("Obtain outputStream array to flush into database blob");
objectSerializable.writeObject(atributos);
log.debug("Writing objet to byte array");
objectSerializable.flush();
log.debug("Serializing object .........");
this.datos = Hibernate.createBlob(output.toByteArray());
log.debug("Setting blob to hibernate scope");
}catch (IOException e) {
log.error("Error to serialize map into blob field \n" +
e.getMessage());
throw new DCMHibernateException(e);
}finally {
log.debug("Cerrando el buffered para el blob");

try {
output.close();
objectSerializable.close();
} catch (IOException e) {
log.error("Unhandler error closing buffer blobs");
e.printStackTrace();
}
}
}

/**
* Este metodo tiene la siguiente funcionalidad:
* Graba los datos en la base de datos pero antes serializa los attributos
*/
public void saveOrUpdate() {
try {
setDatos(this.atributos);
PersistenceManager.saveOrUpdate(this);
} catch (DCMHibernateException e) {
log.error("Error saving sesion\n" + e.getMessage());
}
}


PersistenceManager class is this:


/**
*
* Este metodo tiene la siguiente funcionalidad:
*
*
* @return
*/
private static SessionFactory instancia() {
if (sessionFactory == null) {
try {
Configuration cfg = new Configuration();
log.debug("Iniciando configuraciĆ³n de hibernate");
cfg.configure();
sessionFactory = cfg.buildSessionFactory();
log.debug("Obtienendo SessionFactory ");
} catch (HibernateException e) {
log.error("Error initialize Session Factory hibernate :" +
e.getMessage());
throw new DCMHibernateException(e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}

return sessionFactory;
}

/**
* Obtiene una sesion asociada un thread Local
* @return
*/
public static Session getSession() {
Session s = (Session) threadSession.get();

try {
if (s != null && !s.isOpen()) s = null;
if (s == null) {
log.debug("Session is null - take new Session");
s = instancia().openSession();
threadSession.set(s);
}
} catch (HibernateException ex) {
log.error("Error take a new Session - " + ex.getMessage());
throw new DCMHibernateException(ex);
}

return s;
}


/**
* Actualiza o inserta un objeto en la base segun corresponda
* @param obj
*/
public static void saveOrUpdate(Object obj) {
try {
beginTransaction();
log.debug("Save or Updating object - " + obj.getClass().getName());
getSession().saveOrUpdate(obj);
} catch (HibernateException e) {
try {
Object copy = getSession().merge(obj);
getSession().saveOrUpdate(copy);
} catch (HibernateException ex) {
log.debug("Error save or updating object - " +
obj.getClass().getName() + " \n " + e.getMessage());
throw new DCMHibernateException(e);
}
}
}




Full stack trace of any exception that occurs:

org.hibernate.SessionException: Session is closed!
at org.hibernate.impl.AbstractSessionImpl.errorIfClosed(AbstractSessionImpl.java:50)
at org.hibernate.impl.SessionImpl.getPersistenceContext(SessionImpl.java:1794)
at org.hibernate.proxy.BasicLazyInitializer.getReplacement(BasicLazyInitializer.java:100)
at org.hibernate.proxy.BasicLazyInitializer.invoke(BasicLazyInitializer.java:54)
at org.hibernate.proxy.CGLIBLazyInitializer.intercept(CGLIBLazyInitializer.java:156)
at com.mjoy.dcmbase.entities.Medio$$EnhancerByCGLIB$$8937fbe5.writeReplace(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at java.io.ObjectStreamClass.invokeWriteReplace(ObjectStreamClass.java:896)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1011)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1332)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1304)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1247)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1052)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at java.util.HashMap.writeObject(HashMap.java:978)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at java.io.ObjectStreamClass.invokeWriteObject(ObjectStreamClass.java:809)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1296)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1247)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1052)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:278)
at com.mjoy.dcmbase.entities.Sesion.setDatos(Sesion.java:182)
at com.mjoy.dcmbase.entities.Sesion.saveOrUpdate(Sesion.java:214)
at com.mjoy.dcm.handler.RankingLogic.empiezaTransaccion(Unknown Source)
at com.mjoy.dcm.handler.VentaRankingHandler.execute(Unknown Source)
at com.mjoy.dcm.servlets.DCM.doAction(Unknown Source)
at com.mjoy.dcm.servlets.DCM.doGet(Unknown Source)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:237)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:157)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:214)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:152)
at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)




Name and version of the database you are using: MySql 4.x

Thank for your help


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 4:16 am 
Newbie

Joined: Wed Dec 08, 2004 8:56 am
Posts: 12
Location: China ChengDu
I think your "saveOrUpdate" methoud have something wrong, pls reference doc.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 9:30 am 
Newbie

Joined: Fri Jul 22, 2005 8:47 am
Posts: 8
But my saveOrUpdate() method only call the getSession() that return a new Session Hibernate and call saveOrUpdate() hibernate method.

The extrange is the Exception to be throw before call the saveOrUpdate().

The exception is throw in the method setAtributo(this.atributos) and in this method no call anything in hibernate. Check above for look code and look the method setAtibutos(). I debug the code and the exception is throw excactly in the objectSerializable.writeObject(atributos) line. That you will see this is a call to serialize object atributos that is a Map object.


Please i apreciate your help.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 27, 2006 10:01 am 
Newbie

Joined: Fri Jul 22, 2005 8:47 am
Posts: 8
I think probably i serialize wrong my objet to persist the data.
The extrange is with hibernate 2.x working fine and with hibernte 3.1.1 not.

Please send me any example to serialize objects to blob mysql persist data with hibernte 3.1.1

Thank you


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.