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
|