You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-user@db.apache.org by Leon <le...@hotmail.com> on 2003/05/27 16:26:53 UTC

How to handle SQLException in OJB?

Hi,

In the case of inserting a record with same key value, a SQLException of
duplicate entry is thrown in implementation of OJB. How can I catch this
exception in my DAO object so that I can throw my application exception?
Thanks.

Best regards.

Leon

Re: How to handle SQLException in OJB?

Posted by Leon <le...@hotmail.com>.
Hi Michael,

I tried do that but pbx.getCause() is not PersistenceBrokerSQLException.
pbx.getSourceException() is instance of SQLException and the cause is
"Duplicate entry 'xxxxxx' for key2". I can use this message to do exception
handling. However, I was wandering if it is a generic way to find the
details of SQLException?
Thanks.

Leon
----- Original Message -----
From: "Michael Hart" <mi...@perceptek.com.au>
To: "'OJB Users List'" <oj...@db.apache.org>
Sent: Tuesday, May 27, 2003 9:02 PM
Subject: RE: How to handle SQLException in OJB?


> Hi Leon,
>
> It looks like you're logging level is not set correctly - you need to set
> the logging level of JdbcAccessImpl to FATAL because the printStackTrace
> occurs when set to ERROR or below.
>
> If you're using the default logger (PoorMansLoggerImpl), then you want to
> change your logging levels in OJB.properties:
>
> # Note that this is NOT JdbcAccess, but rather JdbcAccessImpl
> org.apache.ojb.broker.accesslayer.JdbcAccessImpl.LogLevel=FATAL
>
>
> After this, you can catch a PersistenceBrokerException and check whether
it
> is an SQL exception or not:
>
> try
> {
>     Collection allProducts = broker.getCollectionByQuery(query);
> }
> catch (PersistenceBrokerException pbx)
> {
>     // The PersistenceBrokerSQLException is nested in pbx and the
>     // SQLException is nested in that (ie pbx.getCause().getCause())
>     if (pbx.getCause() instanceof PersistenceBrokerSQLException) {
>         System.out.println("SQL problem " + pbx.getCause());
>     }
> }
>
> Cheers,
>
> Michael
>
> -----Original Message-----
> From: Leon [mailto:leonnewsgroup@hotmail.com]
> Sent: Wednesday, 28 May 2003 1:38 AM
> To: OJB Users List
> Subject: Re: How to handle SQLException in OJB?
>
> Here is it. I am using PersistenceBroker.
>
> Thanks.
>
> .......
>
> tx.begin();
>
> broker.store(registryvo);
>
> tx.commit();
>
> } catch (SQLException e) {
>
> e.printStackTrace();
>
> } catch (PersistenceBrokerException e) {
>
> if (log.isEnabledFor(Priority.INFO)) {
>
> log.info(
>
> "********* Something went wrong while insert a record to account
> **********");
>
> }
>
> e.printStackTrace();
>
> throw new DataAccessException("Error in RegistryDAO.insert():" +
>
> e.toString(), e);
>
> }
>
> ----- Original Message -----
> From: "Michael Hart" <mi...@perceptek.com.au>
> To: "'OJB Users List'" <oj...@db.apache.org>
> Sent: Tuesday, May 27, 2003 11:12 AM
> Subject: RE: How to handle SQLException in OJB?
>
>
> > > I already tried that but it is unreachable catch block.
> >
> > It could be from the logger - have you disabled output from that? (or at
> > least set the log-level to something high-ish)
> >
> > The default logger (PoorMansLoggerImpl) will print a stack trace if a
> > Throwable is logged (perhaps log4j does as well), so this may be your
> > problem.
> >
> > Which code is causing the problem? Can you give a sample? Are you using
> > PersistenceBroker or ODMG or JDO?
> >
> > Cheers,
> >
> > Michael
> >
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> > For additional commands, e-mail: ojb-user-help@db.apache.org
> >
> >
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>

RE: How to handle SQLException in OJB?

Posted by Michael Hart <mi...@perceptek.com.au>.
Hi Leon,

It looks like you're logging level is not set correctly - you need to set
the logging level of JdbcAccessImpl to FATAL because the printStackTrace
occurs when set to ERROR or below.

If you're using the default logger (PoorMansLoggerImpl), then you want to
change your logging levels in OJB.properties:

# Note that this is NOT JdbcAccess, but rather JdbcAccessImpl
org.apache.ojb.broker.accesslayer.JdbcAccessImpl.LogLevel=FATAL


After this, you can catch a PersistenceBrokerException and check whether it
is an SQL exception or not:

try
{
    Collection allProducts = broker.getCollectionByQuery(query);
}
catch (PersistenceBrokerException pbx)
{
    // The PersistenceBrokerSQLException is nested in pbx and the 
    // SQLException is nested in that (ie pbx.getCause().getCause())
    if (pbx.getCause() instanceof PersistenceBrokerSQLException) {
        System.out.println("SQL problem " + pbx.getCause());
    }
}

Cheers,

Michael

-----Original Message-----
From: Leon [mailto:leonnewsgroup@hotmail.com] 
Sent: Wednesday, 28 May 2003 1:38 AM
To: OJB Users List
Subject: Re: How to handle SQLException in OJB?

Here is it. I am using PersistenceBroker.

Thanks.

.......

tx.begin();

broker.store(registryvo);

tx.commit();

} catch (SQLException e) {

e.printStackTrace();

} catch (PersistenceBrokerException e) {

if (log.isEnabledFor(Priority.INFO)) {

log.info(

"********* Something went wrong while insert a record to account
**********");

}

e.printStackTrace();

throw new DataAccessException("Error in RegistryDAO.insert():" +

e.toString(), e);

}

----- Original Message -----
From: "Michael Hart" <mi...@perceptek.com.au>
To: "'OJB Users List'" <oj...@db.apache.org>
Sent: Tuesday, May 27, 2003 11:12 AM
Subject: RE: How to handle SQLException in OJB?


> > I already tried that but it is unreachable catch block.
>
> It could be from the logger - have you disabled output from that? (or at
> least set the log-level to something high-ish)
>
> The default logger (PoorMansLoggerImpl) will print a stack trace if a
> Throwable is logged (perhaps log4j does as well), so this may be your
> problem.
>
> Which code is causing the problem? Can you give a sample? Are you using
> PersistenceBroker or ODMG or JDO?
>
> Cheers,
>
> Michael
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-user-help@db.apache.org



Re: How to handle SQLException in OJB?

Posted by Leon <le...@hotmail.com>.
Here is it. I am using PersistenceBroker.

Thanks.

.......

tx.begin();

broker.store(registryvo);

tx.commit();

} catch (SQLException e) {

e.printStackTrace();

} catch (PersistenceBrokerException e) {

if (log.isEnabledFor(Priority.INFO)) {

log.info(

"********* Something went wrong while insert a record to account
**********");

}

e.printStackTrace();

throw new DataAccessException("Error in RegistryDAO.insert():" +

e.toString(), e);

}

----- Original Message -----
From: "Michael Hart" <mi...@perceptek.com.au>
To: "'OJB Users List'" <oj...@db.apache.org>
Sent: Tuesday, May 27, 2003 11:12 AM
Subject: RE: How to handle SQLException in OJB?


> > I already tried that but it is unreachable catch block.
>
> It could be from the logger - have you disabled output from that? (or at
> least set the log-level to something high-ish)
>
> The default logger (PoorMansLoggerImpl) will print a stack trace if a
> Throwable is logged (perhaps log4j does as well), so this may be your
> problem.
>
> Which code is causing the problem? Can you give a sample? Are you using
> PersistenceBroker or ODMG or JDO?
>
> Cheers,
>
> Michael
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
>
>

RE: How to handle SQLException in OJB?

Posted by Michael Hart <mi...@perceptek.com.au>.
> I already tried that but it is unreachable catch block.

It could be from the logger - have you disabled output from that? (or at
least set the log-level to something high-ish)

The default logger (PoorMansLoggerImpl) will print a stack trace if a
Throwable is logged (perhaps log4j does as well), so this may be your
problem.

Which code is causing the problem? Can you give a sample? Are you using
PersistenceBroker or ODMG or JDO?

Cheers,

Michael


Re: How to handle SQLException in OJB?

Posted by Leon <le...@hotmail.com>.
I already tried that but it is unreachable catch block.
Thanx.

Leon
----- Original Message ----- 
From: "Michael Hart" <mi...@perceptek.com.au>
To: "'OJB Users List'" <oj...@db.apache.org>
Sent: Tuesday, May 27, 2003 10:35 AM
Subject: RE: How to handle SQLException in OJB?


> > How can I catch this exception in my DAO object so that I can
> > throw my application exception?
> 
> This doesn't work? :
> 
> try {
>     // DAO code here...
> } catch (SQLException sx) {
>     throw new MyApplicationException("Had an SQL error.");
> }
> 
> What have you tried already?
> 
> Cheers,
> 
> Michael
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: ojb-user-unsubscribe@db.apache.org
> For additional commands, e-mail: ojb-user-help@db.apache.org
> 
> 

RE: How to handle SQLException in OJB?

Posted by Michael Hart <mi...@perceptek.com.au>.
> How can I catch this exception in my DAO object so that I can
> throw my application exception?

This doesn't work? :

try {
    // DAO code here...
} catch (SQLException sx) {
    throw new MyApplicationException("Had an SQL error.");
}

What have you tried already?

Cheers,

Michael