|
 |
servlets thread: How to restart servlet by programming?
Message #1 by Edward <zhangsc@n...> on Fri, 14 Jun 2002 10:02:24 +0800
|
|
thats a bit savage.
No Edward, I am afraid there is nothing built in that will allow you to do
this automatically. Dont connect to the database in the init method is the
answer. Instead write a bean that does it for you and get it to check the
connection before doing sql. THat way, you have the opportunity to reconnect
to the database every time that you use some sql but unless the connection
is bad, it will stay alive all the time.
even better is to use a connection pool. To find out more about connection
pools do some research - search for "Java Connection Pool" in google
chanoch
----- Original Message -----
From: "Nikolas Sakic" <beelzabeb@c...>
To: "Servlets" <servlets@p...>
Sent: Saturday, June 22, 2002 11:19 AM
Subject: [servlets] Re: How to restart servlet by programming?
> Edward,
> First off, you gotta put some effort thinking the solution, thats the
> only way you can learn programming. Do not post any question you get for
> homework- or so it seems.
>
> The database connectiviting classes are in javax.sql.* package.
>
>
>
>
> public class Test extends HttpServlet
> {
> public void init() throws ServletException
> {
> // The quickest and fastest way would be:
>
> String driver = "org.gjt.mm.mysql.Driver";
>
>
> try {
> Class.forName(driver);
> }
> catch(ClassNotFoundException cnfe){
> throw new UnavailableException("Driver <" + driver + "> not found in
> the classpath");
> }
>
>
> protected void doGet(HttpServletRequest req, HttpServletResponse res)
> throws ServletException, IOException {
>
> String username = "john";
> String password = "doe";
> String jdbc_url = "jdbc:mysql://localhost:3306/myDatabase";
> Connection connection = nulll;
> String selectStmt = "select * from myTable";
> Statement statement;
>
> try {
>
> connection = DriverManager.getConnection(url,username,password);
> statement = connection.createStatement();
> ResultSet resultset = statement.executeQuery(selectStmt);
>
>
> while( resultset.next() )
> {
> // print out ur query result
>
> }
>
> // this will close the connection to the database.
> conn.close();
>
> catch(SQLException sqle)
> {
> sqle.printStackTrace();
> }
> catch (Exception allex)
> {
> allex.printStackTrace();
> }
>
> }
>
>
> .
> .
> .
> .
> .
>
> Do whatever u want in the doPost or doGet method. init() is called once
> the servet is initialized so the connention stays open. if not, the
> unavailable exception is thrown. I gave you more than an idea. I think u
> should take it from here. And like i said, this is no way to learn how to
> program if you post every question u come across. You must, first, give it
> a shot at solving.
>
> regards,
>
>
>
>
>
> > In servlet's init() method,I deal with Database operation,when deal
> failed,I throw a UnavailableException,like that:
>
> public class Test extends HttpServlet
> {
> public void init() throws ServletException
> {
> ...
> try
> {
> //Database operation
> ...
> }
> catch(SQLException e)
> {
> throw new UnavailableException("SQL Error");
> }
> }
> public void doPost(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException
> {
> ...
> }
> }
>
> When Database can be used again,I want to know if it exists a method that
> Servlet can know Database can be used and restart servlet again?
> Any idea will be appreciated!
> Edward
|
|
 |