tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From gilmor...@netscape.net
Subject Multi-user problem
Date Thu, 07 Feb 2002 00:55:40 GMT
"Tom Drake" <rtd@pobox.com> wrote:

As suggested, I will try to find a different driver (instead of the odbc/jdbc bridge) to connect
to MSSQL2k.
In the meantime.. I reflect on your other thoughts...

>>See my comments below.
>... snip ...
>|
>| The NEXTIMAGE servlet looks something like this pseudo-code:
>| public static void login(...){ Tell database user is logged in. }
>| public static void logout(...) {Tell database user is logged out.}
>| public static void updateaccesstime(...) {Tell Database I made a request}
>Are the above functions keeping class data
>(such as a connection to your database)?
>
>If a 'login' request gets a new database connection,
>then any other thread that is using
>the original connection will break.

Do other instances of the servlet count as other threads?

I'm not sure I understand what you mean by "keeping class data", so I've copied the 3 methods
here.  In an attempt to answer your question...
1) None of my servlets use multithreading.. I'm new to Java and haven't learned that yet.
2) A database connection is opened as soon as necessary in the servlet and I expect it to
remain available until the servlet closes.  I do not expect it to be available across multiple
instances.
3) The database connection is forced closed at the end of the servlet.

Here's the code for the 3 routines plus the database connect and disconnect (please don't
laugh.. this is the result of a very steep learning curve and a bunch of troubleshooting).
 As you see, the login/out methods expect an existing statement (which is bound to a connection)
as a parameter:


    //---------------------------------------------------
    public static void login(String userid, Statement st) {
        String s="";
        System.err.println("Login");
        try {
            s=
            "use sqaa "+
            "update emps "+
            "set loggedin=1, "+
            "lastaccess='"+thelper.getcurtime()+"' "+
            "where userid='"+userid+"' ";
            st.executeUpdate(s);
        } catch (Exception exc) {
            System.err.println("Error during login().");
            System.err.println("  "+exc.getClass().getName());
            System.err.println("  "+exc.getMessage());
            System.err.println(s);
            return;
        }
    }
    //------------------------------------------------------------
    public static void logout(int userrecordid, String userid, Statement st) {
        String s="";
        System.err.println("Logout");
        try {
            System.err.println("..logging out");
            s=
            "use sqaa "+
            "update emps "+
            "set loggedin=0 "+
            "where userid='"+userid+"' ";
            st.executeUpdate(s);

            System.err.println("..cancelling reservations for "+userrecordid+", "+userid);
            s=
            "use sqaa "+
            "update images "+
            "set auditorid=null, auditorstatus='U' "+
            "where auditorid="+userrecordid+" "+
            "and auditorstatus='X'";
            st.executeUpdate(s);

            System.err.println("Done logging out.");

        } catch (Exception exc) {
            System.err.println("Error during logout.");
            System.err.println("  "+exc.getClass().getName());
            System.err.println("  "+exc.getMessage());
            System.err.println(s);
            return;
        }
    }
    //------------------------------------------------------------
    public static void updateaccesstime(String userid, Statement st) {
        String s="";
        System.err.println("Updateaccesstime");
        s=
            "use sqaa "+
            "update emps "+
            "set lastaccess = '"+thelper.getcurtime()+"' "+
            "where userid='"+userid+"' ";
        try {
        st.executeUpdate(s);

        } catch (Exception exc) {
            System.err.println("Error during updateaccesstime().");
            System.err.println("  "+exc.getClass().getName());
            System.err.println("  "+exc.getMessage());
            System.err.println(s);
            return;
        }
    }


        //Open JDBC Connection (This occurs right after getting the POST parameters):
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            con = DriverManager.getConnection("jdbc:odbc:sqaa","philg","");
            query = con.createStatement();
        }
        catch (Exception exc) {
            System.err.println("Error creating JDBC connection.");
            System.err.println(exc.getClass().getName());
            System.err.println(exc.getMessage());
            thelper.fileServe(out,"c:/html/except.html");
            return;
        }

        //Close database connection (this is the last thing done).
        try{
        con.close();
        } catch (Exception exc) {
            System.err.println("Error closing database.");
            System.err.println(exc.getClass().getName());
            System.err.println(exc.getMessage());
            thelper.fileServe(out,"c:/html/except.html");
            return;
        }

-- 




__________________________________________________________________
Your favorite stores, helpful shopping tools and great gift ideas. Experience the convenience
of buying online with Shop@Netscape! http://shopnow.netscape.com/

Get your own FREE, personal Netscape Mail account today at http://webmail.netscape.com/


--
To unsubscribe:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands: <mailto:tomcat-user-help@jakarta.apache.org>
Troubles with the list: <mailto:tomcat-user-owner@jakarta.apache.org>


Mime
View raw message