Hi Phil, Franck,
The code you are using might work in some situations but it will fail in others. It would only work if you call from a bean another bean with a "Required" transaction descriptor. If you call from a bean another bean with "RequiresNew" transaction descriptor your transaction will be messed up since by default SimpleORM only handles one JDBC connection attached to one transaction (stored in a thread local object) and not two different ones like in this case (Required -> RequiresNew). Also it might not work in certain multithreaded (multi JVM) scenarios.
I would suggest to use SConnectionEJB instead which enhances the framework with multiple connections/transactions. Also it takes care of automatic flush during commit and automatic detach.
This way your code would look like this:
InitialContext initCtx = new InitialContext();
DataSource ds = (javax.sql.DataSource)
initCtx.lookup("java:/DefaultDS");
Connection con = ds.getConnection();
SConnectionEJB.attach(con, name);
try {
String paramValue = <params>.getParameter("accountId");
acct = (Account) Account.meta.findOrCreate(paramValue);
if (acct.isNewRow() == false) { // found !
acct.detach();
found = true;
}
} catch (Exception ex) {
log("SORM Error " + ex);
}
Notice that you don't have to call begin, flush, detach.
Regarding your specific problem: in Jboss you have to check your setting in server/default/deploy/transaction-service.xml.
Bellow is what you should have (and the comment from Jboss which explains it):
<!--SpecCompliant false means JBoss will close connections left open when you return from a method call and generate a loud warning. SpecCompliant true means JBoss will disconnect connection handles left open on return from a method call and reconnect them with an appropriate (security, tx) connection on the next call to the same object.-->
<attribute name="SpecCompliant">true</attribute>
If you need more information please let me know.
Regards,
Dan.
-----Original Message-----
From: Philippe Back (High Octane) [mailto:phil <at> highoctane.be]
Sent: Monday, February 07, 2005 8:10 AM
To: SimpleORM <at> yahoogroups.com
Subject: Re: [SimpleORM] EJB
Hi Franck,
Thanks!
I do not understand why having the close in the finally instead of inside
the try block would do any difference for JBoss (I understand why it has to
be in the finally anyhow).
My current issue is rather linked to the fact that my EJB is configured as
Container in the ejb-jar.xml.
I would rather use Spring and let go of the EJBs but before doing that, I've
to make it work in the EJB container... The goal being to prove that EJB
have a lot of issues that can be avoided by using something lighter. So, I
am basically building a case.
Best regards,
/Phil
----- Original Message -----
From: "Franck Routier" <franck.routier <at> axege.com>
To: <SimpleORM <at> yahoogroups.com>
Sent: Monday, 07 February, 2005 09:00
Subject: Re: [SimpleORM] EJB
> Hi,
>
> this is what I did (once upon a time):
>
>
> try {
> /* SimpleORM specific code
> InitialContext ctx = new InitialContext();
> DataSource ds = (javax.sql.DataSource)
> ctx.lookup("java:PostgresDS");
> con = ds.getConnection();
> SConnection.attach(con, "getAllEcr");
> SConnection.begin();
> .......
> SConnection.flush();
> SConnection.detachWithoutClosing();
> //con.close(); prevents jboss from sending exception (done
> in finally)
>
> } catch (Exception e) {
> ....
> } finally {
> try {
> if (con != null) {
> con.close();
> }
> } catch (Exception e) {
> // Container will take care of that
> }
> }
>
> Maybe this can help.
>
> Franck
>
> Philippe Back (High Octane) a écrit :
> > I am currently leveraging SimpleORM in an EJB component.
> > I can perform the calls and all.
> >
> > In the bean I use:
> >
> > /**
> > * Gets current connection to the connection pool.
> > */
> >
> > private void attachConnection(String name) throws Exception {
> > InitialContext initCtx = new InitialContext();
> > DataSource ds = (javax.sql.DataSource)
> > initCtx.lookup("java:/DefaultDS"); // TODO: put this in a parm in the
xml
> > descriptor
> > Connection con = ds.getConnection();
> > SConnection.attach(con, name);
> > SConnection.begin(); // do not start a new transac
> > }
> >
> > and in a business method I do:
> >
> > try {
> > attachConnection("InfoSystemCon");
> > String paramValue = <params>.getParameter("accountId");
> > acct = (Account) Account.meta.findOrCreate(paramValue);
> > if (acct.isNewRow() == false) { // found !
> > acct.detach();
> > found = true;
> > }
> > } catch (Exception ex) {
> > log("SORM Error " + ex);
> > } finally {
> > SConnection.detachWithoutClosing(); // managed cnx
> > }
> >
> > I get my result and all but JBoss says that he had to close a connection
for
> > me.
> >
> > Do I have to do something more on the connection ? If I close it, JBoss
> > complains that I shouldn't be doing it...
> >
> > Do you have any experience regarding this behavior ?
> >
> > Dan, I will try your code for SConnectionEJB, this sample is not using
it
> > yet.
> >
> > Maybe would you be so kind as to provide a sample EJB code that works
with
> > SimpleORM and JBoss. This will help tremendously.
> >
> > For the record, the SimpleORM generator works damn fine !
> >
> > All the best.
> > /Philippe Back
> > www.highoctane.be
> >
> >
> > ------------------------------------------------------------------------
> > *Yahoo! Groups Links*
> >
> > * To visit your group on the web, go to:
> > http://groups.yahoo.com/group/SimpleORM/
> >
> > * To unsubscribe from this group, send an email to:
> > SimpleORM-unsubscribe <at> yahoogroups.com
> > <mailto:SimpleORM-unsubscribe <at> yahoogroups.com?subject=Unsubscribe>
> >
> > * Your use of Yahoo! Groups is subject to the Yahoo! Terms of
> > Service <http://docs.yahoo.com/info/terms/>.
> >
> >
>
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/SimpleORM/
<*> To unsubscribe from this group, send an email to:
SimpleORM-unsubscribe <at> yahoogroups.com
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/