Transactions?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Transactions?

Neal Sanche
Hi All,

Okay, another rough spot found in my transition. I'm trying to create a
new CMP bean instance from my web app. But it needs a transaction to do
so. I suppose I could do the operation from within a Session bean method
(I probably will anyway), but is there a way to obtain a new Transaction
from within the web application's code? I was used to doing the following:

        UserTransaction tx = null;
        InitialContext ctx = null;
        try {
            ctx = new InitialContext();
            tx = (UserTransaction)ctx.lookup("UserTransaction");
            tx.begin();
            chain.doFilter(request, response);
        } catch (Throwable e) {
            throw new ServletException(e);
        } finally {
            try {
                if (tx != null)
                    tx.commit();
                if (ctx != null)
                    ctx.close();
            } catch (Throwable ex) {
                throw new ServletException(ex);
            }
        }

Now that just throws a name not found for UserTransaction. How do I do
it with Geronimo?

Thanks in advance.

-Neal
Reply | Threaded
Open this post in threaded view
|

Re: Transactions?

Jeff Genender
Try to lookup "java:comp/UserTransaction"

Jeff

Neal Sanche wrote:

> Hi All,
>
> Okay, another rough spot found in my transition. I'm trying to create a
> new CMP bean instance from my web app. But it needs a transaction to do
> so. I suppose I could do the operation from within a Session bean method
> (I probably will anyway), but is there a way to obtain a new Transaction
> from within the web application's code? I was used to doing the following:
>
>        UserTransaction tx = null;
>        InitialContext ctx = null;
>        try {
>            ctx = new InitialContext();
>            tx = (UserTransaction)ctx.lookup("UserTransaction");
>            tx.begin();
>            chain.doFilter(request, response);
>        } catch (Throwable e) {
>            throw new ServletException(e);
>        } finally {
>            try {
>                if (tx != null)
>                    tx.commit();
>                if (ctx != null)
>                    ctx.close();
>            } catch (Throwable ex) {
>                throw new ServletException(ex);
>            }
>        }
>
> Now that just throws a name not found for UserTransaction. How do I do
> it with Geronimo?
>
> Thanks in advance.
>
> -Neal
Reply | Threaded
Open this post in threaded view
|

Re: Transactions?

David Jencks
In reply to this post by Neal Sanche
I think the portable location is java:comp/UserTransaction

In geronimo we pretty much follow the spec that you can only look
things up under java:comp/

thanks
david jencks

On Jun 17, 2005, at 12:04 AM, Neal Sanche wrote:

> Hi All,
>
> Okay, another rough spot found in my transition. I'm trying to create
> a new CMP bean instance from my web app. But it needs a transaction to
> do so. I suppose I could do the operation from within a Session bean
> method (I probably will anyway), but is there a way to obtain a new
> Transaction from within the web application's code? I was used to
> doing the following:
>
>        UserTransaction tx = null;
>        InitialContext ctx = null;
>        try {
>            ctx = new InitialContext();
>            tx = (UserTransaction)ctx.lookup("UserTransaction");
>            tx.begin();
>            chain.doFilter(request, response);
>        } catch (Throwable e) {
>            throw new ServletException(e);
>        } finally {
>            try {
>                if (tx != null)
>                    tx.commit();
>                if (ctx != null)
>                    ctx.close();
>            } catch (Throwable ex) {
>                throw new ServletException(ex);
>            }
>        }
>
> Now that just throws a name not found for UserTransaction. How do I do
> it with Geronimo?
>
> Thanks in advance.
>
> -Neal
>

Reply | Threaded
Open this post in threaded view
|

Re: Transactions?

Dain Sundstrom
In reply to this post by Neal Sanche
On Jun 17, 2005, at 12:04 AM, Neal Sanche wrote:

>        UserTransaction tx = null;
>        InitialContext ctx = null;
>        try {
>            ctx = new InitialContext();
>            tx = (UserTransaction)ctx.lookup("UserTransaction");
>            tx.begin();
>            chain.doFilter(request, response);
>        } catch (Throwable e) {
>            throw new ServletException(e);
>        } finally {
>            try {
>                if (tx != null)
>                    tx.commit();
>                if (ctx != null)
>                    ctx.close();
>            } catch (Throwable ex) {
>                throw new ServletException(ex);
>            }
>        }

Is this a servlet filter?  I don't believe it is legal to propagate a  
tx out of a servlet filter (either down the chain or up the chain).

-dain
Reply | Threaded
Open this post in threaded view
|

Re: Transactions?

Neal Sanche
Dain Sundstrom wrote:

> On Jun 17, 2005, at 12:04 AM, Neal Sanche wrote:
>
>>        UserTransaction tx = null;
>>        InitialContext ctx = null;
>>        try {
>>            ctx = new InitialContext();
>>            tx = (UserTransaction)ctx.lookup("UserTransaction");
>>            tx.begin();
>>            chain.doFilter(request, response);
>>        } catch (Throwable e) {
>>            throw new ServletException(e);
>>        } finally {
>>            try {
>>                if (tx != null)
>>                    tx.commit();
>>                if (ctx != null)
>>                    ctx.close();
>>            } catch (Throwable ex) {
>>                throw new ServletException(ex);
>>            }
>>        }
>
>
> Is this a servlet filter?  I don't believe it is legal to propagate a  
> tx out of a servlet filter (either down the chain or up the chain).
>
> -dain

Really? I guess it's not very kosher, but it certainly helps simplify
the code for the pages since while pages render, there's always a
transaction available. There are some obvious side-effects. This 'not
legal' statement is from a document somewhere Dain?

-Neal
Reply | Threaded
Open this post in threaded view
|

Re: Transactions?

Dain Sundstrom
On Jun 17, 2005, at 6:11 PM, Neal Sanche wrote:

> Dain Sundstrom wrote:
>
>> On Jun 17, 2005, at 12:04 AM, Neal Sanche wrote:
>>
>>>        UserTransaction tx = null;
>>>        InitialContext ctx = null;
>>>        try {
>>>            ctx = new InitialContext();
>>>            tx = (UserTransaction)ctx.lookup("UserTransaction");
>>>            tx.begin();
>>>            chain.doFilter(request, response);
>>>        } catch (Throwable e) {
>>>            throw new ServletException(e);
>>>        } finally {
>>>            try {
>>>                if (tx != null)
>>>                    tx.commit();
>>>                if (ctx != null)
>>>                    ctx.close();
>>>            } catch (Throwable ex) {
>>>                throw new ServletException(ex);
>>>            }
>>>        }
>>
>> Is this a servlet filter?  I don't believe it is legal to  
>> propagate a  tx out of a servlet filter (either down the chain or  
>> up the chain).
>>
>> -dain
>>
>
> Really? I guess it's not very kosher, but it certainly helps  
> simplify the code for the pages since while pages render, there's  
> always a transaction available. There are some obvious side-effects.

Well, I don't think this works on all platforms.  I remember  
Websphere being very very very strict about transactions in the web  
tier.  One reason to not wrap an entire web request in a transaction  
is you could end up holding the transaction open for a long time as  
data is force flushed to a modem user.  It is best to read/update all  
the data in one isolated tight transaction and then render the page.  
To maximize the transaction throughput you want to hold the  
transaction open as short as possible.

Having said that.  People do this all the time, and it should work in  
Geronimo.  It is way to common a pattern to not support.

> This 'not legal' statement is from a document somewhere Dain?

It is somewhere in the servlet spec.  Just search for transaction.  
They hardly ever mention transactions so it should be easy to find.  
If it isn't in the servlet spec, check the J2EE spec.

-dain