Skip to main content

Glassfish, JSPs and Java classes. How static is static?

1 reply [Last post]
growse
Offline
Joined: 2009-07-27
Points: 0

I've been playing with Glassfish recently and getting to know Java a bit better. I've been building a fairly simple website and had some questions.

So, my model is this: I've got some JSPs (tier 1, effectively). These then call methods within classes in various classes / packages (tier 2). The database conneciton (tier 3) is created in the glassfish admin console as a JDBC resource - when one of the methods needs to access the db, it calls this:

<br />
public class DataConnection {<br />
    public  static DataSource getDevdb() throws NamingException {<br />
        Context c = new InitialContext();<br />
        return (DataSource) c.lookup("java:comp/env/devdb");<br />
    }<br />
}<br />

It's not clear to me whether the methods in the tier 2 classes should be static or not. All they're doing is getting some data out of the database based on parameters given to them, chucking it in an object and returning it to the JSP, which then formats the output. Within the context of the application server, how static is a static variable? Is there one per request? Per thread? One for the entire server instance? If the tier 2 methods are static, will an executing request block all further requests until that method is complete?

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Jim Driscoll

On 7/27/09 4:18 AM, webtier@javadesktop.org wrote:

> It's not clear to me whether the methods in the tier 2 classes should be static or not. All they're doing is getting some data out of the database based on parameters given to them, chucking it in an object and returning it to the JSP, which then formats the output. Within the context of the application server, how static is a static variable? Is there one per request? Per thread? One for the entire server instance? If the tier 2 methods are static, will an executing request block all further requests until that method is complete?

It's the same as any VM - nothing special, since the static modifier is
a VM property.

But you're mixing up method and variable. The two are quite different,
naturally. You show a method, but then ask about variables. Which do
you mean?

Static methods won't be any problem for you. There's no concurrency
problem associated with using them. So the code you show should be fine.

Jim

---------------------------------------------------------------------
To unsubscribe, e-mail: webtier-unsubscribe@glassfish.dev.java.net
For additional commands, e-mail: webtier-help@glassfish.dev.java.net