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



  •  I'm going to ask this on the java.net forums as well, but given there's a bunch of smart people here, I thought I'd ask here as well. I've been playing about with Glassfish 2.0, building some simple web apps. I'm reasonably new to Java, but am more experienced in C# .NET so it's not alltogether unfamiliar.

    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:

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

     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?



  • @growse said:

     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?

     

    You don't have any static variables here.  All you have is a static method which means that the method is bound to the class, not to the object.  This doesn't really mean too much except that you loose any abilities to override the method in a subclass (no a very useful thing to do 95% of the time) and that the method can only use static members of the class (but you don't have any, so it doesn't matter).

    This will not block subsequent requests, (you need you use the synchronized attribute on the method for that).

    As for is this the right way to do it, it depends on your needs.  If you need to plug in different tier 3s at runtime this might not be a very good way (think, failover to 2nd server, running in debug mode on a test environment).  Considering that this is a website, these issues will probably not be very common, so static methods will work fine.



  •  Ah, I got confused. You're right of course. I've not run into any issues so far, so I'll stick with static methods until I run into problems.


Log in to reply