logo       

roller/src/org/roller/business/hibernate HibernateStrategy.java,1.5,1.6: msg#00063

java.roller.cvs

Subject: roller/src/org/roller/business/hibernate HibernateStrategy.java,1.5,1.6

Update of /cvsroot/roller/roller/src/org/roller/business/hibernate
In directory sc8-pr-cvs1:/tmp/cvs-serv28933/src/org/roller/business/hibernate

Modified Files:
HibernateStrategy.java
Log Message:
Gavin: "make sure you never catch + handle an exception and
then keep using the session (ObjectNotFoundException included!)"

Index: HibernateStrategy.java
===================================================================
RCS file:
/cvsroot/roller/roller/src/org/roller/business/hibernate/HibernateStrategy.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -C2 -d -r1.5 -r1.6
*** HibernateStrategy.java 9 Sep 2003 11:42:58 -0000 1.5
--- HibernateStrategy.java 14 Sep 2003 19:00:58 -0000 1.6
***************
*** 4,11 ****
package org.roller.business.hibernate;

- import java.util.List;
-
import net.sf.hibernate.HibernateException;
- import net.sf.hibernate.ObjectNotFoundException;
import net.sf.hibernate.Session;
import net.sf.hibernate.SessionFactory;
--- 4,8 ----
***************
*** 18,21 ****
--- 15,20 ----
import org.roller.pojos.PersistentObject;

+ import java.util.List;
+


///////////////////////////////////////////////////////////////////////////////
***************
*** 32,35 ****
--- 31,37 ----
private static Log mLogger =
LogFactory.getFactory().getInstance(HibernateStrategy.class);
+
+
+
//-------------------------------------------------------------------------

public HibernateStrategy(SessionFactory factory) throws RollerException
***************
*** 38,46 ****
}

private boolean isAvailable() {
return mSessionTLS.get()!=null;
}

!
protected Session getSession()
{
--- 40,52 ----
}

+
//-------------------------------------------------------------------------
+
private boolean isAvailable() {
return mSessionTLS.get()!=null;
}

!
//-------------------------------------------------------------------------
!
! /** get session for current thread */
protected Session getSession()
{
***************
*** 51,82 ****
{
ses = mSessionFactory.openSession();
! mLogger.debug("OPENED session "+ses);
}
catch (HibernateException e)
{
! mLogger.error("ERROR opening session");
throw new RuntimeException();
}
mSessionTLS.set(ses);
! mLogger.debug("ADDED session to TLS"+ses);
}
return ses;
}

public void release() throws RollerException
{
try
{
! mLogger.debug("CLOSING session "+getSession());
! getSession().flush();
! getSession().close();
!
! mLogger.debug("REMOVING session from TLS"+getSession());
! mSessionTLS.set(null);
}
! catch (HibernateException e)
{
! mLogger.error("During CLOSE",e);
! throw new RollerException(e);
}
}
--- 57,106 ----
{
ses = mSessionFactory.openSession();
! mLogger.debug(Messages.getString(
! "HibernateStrategy.openedSession")+ses);
}
catch (HibernateException e)
{
! mLogger.error(Messages.getString(
! "HibernateStrategy.errorOpeningSession"));
throw new RuntimeException();
}
mSessionTLS.set(ses);
! mLogger.debug(Messages.getString(
! "HibernateStrategy.addedSession")+ses);
}
return ses;
}

+
//-------------------------------------------------------------------------
+
+ /** remove session from thread, flush it and close it. */
public void release() throws RollerException
{
+ Session ses = (Session)mSessionTLS.get();
+
+ if ( null == ses ) return; // there is no session to release
+
+ mSessionTLS.set(null); // sets thread's session to null
+
try
{
! ses.flush();
}
! catch (HibernateException he)
{
! mLogger.error(Messages.getString(
! "HibernateStrategy.duringRelease"),he);
! throw new RollerException(he);
! }
! try
! {
! ses.close();
! }
! catch (HibernateException he)
! {
! mLogger.error(Messages.getString(
! "HibernateStrategy.duringRelease"),he);
! throw new RollerException(he);
}
}
***************
*** 94,115 ****
if ( id == null )
{
! throw new RollerException("NULL is not a valid id");
}

if ( clazz == null )
{
! throw new RollerException("NULL is not a valid class");
}

try
{
! // Create persistent instance and delete it
! Object obj = getSession().load(clazz,id);
getSession().delete(obj);
getSession().flush();
}
! catch (Throwable e)
! {
! mLogger.error("During DELETE",e);
throw new RollerException(e);
}
--- 118,149 ----
if ( id == null )
{
! throw new RollerException(Messages.getString(
! "HibernateStrategy.nullNotValidId"));
}

if ( clazz == null )
{
! throw new RollerException(Messages.getString(
! "HibernateStrategy.nullNotValidClass"));
}

+ // Create persistent instance and delete it
+ Object obj;
try
{
! obj = getSession().load(clazz,id);
getSession().delete(obj);
getSession().flush();
}
! catch (HibernateException e)
! {
! String msg = Messages.formatString(
! "HibernateStrategy.errorRemoving",id,clazz.getName());
! mLogger.error(msg);
!
! // Gavin: "make sure you never catch + handle an exception and
! // then keep using the session (ObjectNotFoundException
included!)"
! release();
!
throw new RollerException(e);
}
***************
*** 129,138 ****
if ( id == null )
{
! throw new RollerException("NULL is not a valid id");
}

if ( clazz == null )
{
! throw new RollerException("NULL is not a valid class");
}

--- 163,174 ----
if ( id == null )
{
! throw new RollerException(Messages.getString(
! "HibernateStrategy.nullNotValidId"));
}

if ( clazz == null )
{
! throw new RollerException(Messages.getString(
! "HibernateStrategy.nullNotValidClass"));
}

***************
*** 143,160 ****
obj = (PersistentObject)ses.load( clazz, id );
}
- catch (ObjectNotFoundException ignored)
- {
- // Roller persistence managers are supposed to return null
- // if the requested object is not found.
- }
catch (HibernateException e)
! {
! String msg = "During LOAD of "+id+"of class "+clazz.getName();
! mLogger.warn(msg,e);
! }
! catch (Throwable e)
! {
! mLogger.error("During LOAD",e);
! throw new RollerException(e);
}
return (PersistentObject)obj;
--- 179,191 ----
obj = (PersistentObject)ses.load( clazz, id );
}
catch (HibernateException e)
! {
! String msg = Messages.formatString(
! "HibernateStrategy.errorRetrieving",id,clazz.getName());
! mLogger.error(msg);
!
! // Gavin: "make sure you never catch + handle an exception and
! // then keep using the session (ObjectNotFoundException
included!)"
! release();
}
return (PersistentObject)obj;
***************
*** 172,176 ****
if ( obj == null )
{
! throw new RollerException("NULL passed into storeValueObject()");
}

--- 203,208 ----
if ( obj == null )
{
! throw new RollerException(Messages.getString(
! "HibernateStrategy.nullPassedIn"));
}

***************
*** 178,186 ****
try
{
! if ( obj.getId() == null || obj.getId().trim().equals("") )
{
// Object has never been written to database, so save it.
// This makes obj into a persistent instance.
- obj.setId(null);
ses.save(obj);
}
--- 210,219 ----
try
{
! // Dave: I tried using ses.saveOrUpdate() here, but it did not
work.
!
! if ( obj.getId() == null || obj.getId().trim().equals("") )
{
// Object has never been written to database, so save it.
// This makes obj into a persistent instance.
ses.save(obj);
}
***************
*** 190,209 ****
// Object has been written to database, but instance passed in
// is not a persistent instance, so must be loaded into
session.
! PersistentObject vo =
(PersistentObject)ses.load(obj.getClass(),obj.getId());
vo.setData(obj);
obj = vo;
}
-
- //ses.saveOrUpdate(obj);
-
- // Write current session state to database
- //ses.flush();
-
- //ses.connection().commit(); // TODO can't call when
autocommit=true
}
! catch (Exception e) // HibernateException or SQLException
{
! String msg = "ERROR storing object";
! mLogger.error(msg,e);
throw new RollerException(msg,e);
}
--- 223,242 ----
// Object has been written to database, but instance passed in
// is not a persistent instance, so must be loaded into
session.
! PersistentObject vo =
! (PersistentObject)ses.load(obj.getClass(),obj.getId());
vo.setData(obj);
obj = vo;
}
}
! catch (HibernateException e)
{
! String msg = Messages.formatString(
! "HibernateStrategy.errorStoring",obj.getId());
! mLogger.error(msg);
!
! // Gavin: "make sure you never catch + handle an exception and
! // then keep using the session (ObjectNotFoundException
included!)"
! release();
!
throw new RollerException(msg,e);
}
***************
*** 213,221 ****

//-------------------------------------------------------------------------

! public List query( String query, Object[] args, Object[] types) throws
RollerException
{
return query(query, args, (Type[])types);
}

public List query( String query, Object[] args, Type[] types )
throws RollerException
--- 246,257 ----

//-------------------------------------------------------------------------

! public List query( String query, Object[] args, Object[] types)
! throws RollerException
{
return query(query, args, (Type[])types);
}

+
//-------------------------------------------------------------------------
+
public List query( String query, Object[] args, Type[] types )
throws RollerException
***************
*** 223,244 ****
if ( query == null )
{
! throw new RollerException("NULL is not a valid query");
}

if ( args == null )
{
! throw new RollerException("NULL is not a valid arg array");
}

if ( types == null )
{
! throw new RollerException("NULL is not a valid type array");
}

try
{
! if (query.indexOf("$") > -1)
{
! query = query.replaceAll("\\$\\d+", "\\?");
}
return getSession().find(query,args,types);
--- 259,283 ----
if ( query == null )
{
! throw new RollerException(Messages.getString(
! "HibernateStrategy.nullNotValidQuery"));
}

if ( args == null )
{
! throw new RollerException(Messages.getString(
! "HibernateStrategy.nullNotValidArgArray"));
}

if ( types == null )
{
! throw new RollerException(Messages.getString(
! "HibernateStrategy.nullNotValidArrayType"));
}

try
{
! if (query.indexOf("$") > -1)
{
! query = query.replaceAll("\\$\\d+", "\\?");
}
return getSession().find(query,args,types);
***************
*** 246,254 ****
catch (HibernateException e)
{
! mLogger.error("During QUERY",e);
! throw new RollerException("During QUERY",e);
}
}

public List query( String query )
throws RollerException
--- 285,296 ----
catch (HibernateException e)
{
! String msg =
Messages.getString("HibernateStrategy.duringQuery");
! mLogger.error(msg,e);
! throw new RollerException(msg,e);
}
}

+
//-------------------------------------------------------------------------
+
public List query( String query )
throws RollerException
***************
*** 256,262 ****
try
{
! if (query.indexOf("$") > -1)
{
! query = query.replaceAll("\\$\\d+", "\\?");
}
return getSession().find(query);
--- 298,304 ----
try
{
! if (query.indexOf("$") > -1)
{
! query = query.replaceAll("\\$\\d+", "\\?");
}
return getSession().find(query);
***************
*** 264,278 ****
catch (HibernateException e)
{
! mLogger.error("During QUERY",e);
! throw new RollerException("During QUERY",e);
}
}

! /**
! *
! */
public void commit() throws RollerException
{
! //if (mTranx != null) mTranx.commit();
try {
if (isAvailable()) getSession().flush();
--- 306,320 ----
catch (HibernateException e)
{
! String msg = Messages.getString("HibernateStrategy.duringQuery");
! mLogger.error(msg,e);
! throw new RollerException(msg,e);
}
}

!
//-------------------------------------------------------------------------
!
public void commit() throws RollerException
{
!
try {
if (isAvailable()) getSession().flush();
***************
*** 283,290 ****
}

public void rollback() throws RollerException
{
- //if (mTranx != null) mTranx.rollback();
- //if (isAvailable()) mSession.connection().rollback();
}

--- 325,332 ----
}

+
//-------------------------------------------------------------------------
+
public void rollback() throws RollerException
{
}





-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf


<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise