1. init method (1 messages)

    hi
      i m initializing db connections in the init method(that's what i presume every one do) of the servlet. My question is while establishing connection, if db crashes or some error occurs, how could i prevent the init method to be loaded.


    regards
    sundar

    Threaded Messages (1)

  2. init method[ Go to top ]

    If you do that, then I assume your DB connections are member variables of the servlet?

    If that's the case then you must synchronize access to the connections, since there will be many many threads of execution running through your servlets service(request, response) method.

    I actually would not do that in the init method, let the server handle the connection pooling itself.

    On the other hand, if you are using a servlet engine that is not an application server, then I would put the code in the init method of a servlet that does nothing (It rejects all requests with a 400 code) and exists solely to start up things for the server.

    Then you list this servlet as a startup servlet to the engine (One that is loaded when the server starts, not when the first request comes in, which is the standard approach.)

    In either case, if you get an exception during the init method you have two options.

    1) Swallow it and let the server fall apart.
    2) System.exit(1).

    Option (2) is not as terrible as it sounds, since if you can't talk to the database then there's probably nothing you can do.

    Alternatively, make your init method set a flag in some Singleton class which indicates that the system is initialized. Your other servlets can check it and return the standard service unavailable code if it is not set to true.

    That should take care of it.

    Chz

    Tony