Programmatically detecting login or logout events
Hello I'm trying to find a way to detect the events caused when a user logs into or logs out of an application I'm working on. I want to record these events so that I know the exact steps a user has taken through the application's JSPs and servlets. I want to do this without reference to the Apache Tomcat server logs as well. I have tried the following code in index.jsp (which is serving as each user's home page): <% if (request.getParameter("logoff") != null) { session.invalidate(); response.sendRedirect("/myDataSharer/jsp/user/index.jsp"); return; } %> And I have tried using variations like: <% if (request.getParameter("logon") == true) { But without success. I have used request.getRemoteUser() at various points in the application to identify the current user when they are authenticated by Apache Tomcat. However, request.getRemoteUser() does not tell me when the user logged in or off. Does anyone know a way that the login and logout events can be recorded by an event in a program, a session object and so on? I'm using NetBeans 6.1 with Apache Tomcat 6.X. Thanks Martin O'Shea. -- View this message in context: http://www.nabble.com/Programmatically-detecting-login-or-logout-events-tp18685933p18685933.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Programmatically detecting login or logout events
markt-2 wrote: > > MartinOShea wrote: >> I have used request.getRemoteUser() at various points in the application >> to >> identify the current user when they are authenticated by Apache Tomcat. >> However, request.getRemoteUser() does not tell me when the user logged >> in >> or off. > request.getRemoteUser() tells you if the user is authenticated. > >> Does anyone know a way that the login and logout events can be recorded >> by >> an event in a program, a session object and so on? > Have you looked at session listeners? > > It depends what you mean by login and logout. > If login is the transition from request.getRemoteUser()==null to > request.getRemoteUser()!=null and logout is when the session is > invalidated > then a combination of request.getRemoteUser() and a session listener > should > be all you need. > > Mark > > > > - > To start a new topic, e-mail: users@tomcat.apache.org > To unsubscribe, e-mail: [EMAIL PROTECTED] > For additional commands, e-mail: [EMAIL PROTECTED] > > > Mark By login or logout, I mean the actual physical login and logout actions by the user concerned which I expect translate to request.getRemoteUser()==null for logout and request.getRemoteUser()!=null for login. But I'm not familiar with session listeners so I will be looking at them later. Do you have any sample code at all? Thanks Martin O'Shea. -- View this message in context: http://www.nabble.com/Programmatically-detecting-login-or-logout-events-tp18685933p18686159.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Programmatically detecting login or logout events
Chris Thanks for the reply. In the various servlets making up this application, I don't currently have a user object in every request but tend to use request.getRemoteUser() where necessary. This also to minimizes traffic. But what you've suggested is good. Thanks Martin O'Shea. -- View this message in context: http://www.nabble.com/Programmatically-detecting-login-or-logout-events-tp18685933p18770154.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To start a new topic, e-mail: users@tomcat.apache.org To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Tomcat 6.X and MySQL connection pooling issue
Hello I wonder if anyone can advise me on this issue. I have a Tomcat 6.X Java / JSP application which uses connection pooling to access a MySQL database but, if the application is left for up to eight hours, one of the pages fails to display the contents of a dataset upon loading. Looking into the logs, I find that I have the following: ERROR|29 09 2009|08 42 19|http-8080-4|myDataSharer.database_access.Database_Metadata_DBA| - Error getting types of columns of tabular Dataset 12 com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception: ** BEGIN NESTED EXCEPTION ** java.io.EOFException STACKTRACE: java.io.EOFException at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1956) at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2368) at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2867) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616) at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708) at com.mysql.jdbc.Connection.execSQL(Connection.java:3255) at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1293) at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1428) at org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93) at myDataSharer.database_access.Database_Metadata_DBA.getTabDSColumnTypes(Database_Metadata_DBA.java:364) at myDataSharer.database_access.Dataset_DBA.getTabDSAsHTMLTable(Dataset_DBA.java:1266) And so on for several hundred lines. Now the page in question is meant to display data from one of a number of tables in MySQL which vary in their column types. Each table forms a dataset which is created from data extracted from RSS feeds or an Excel / CSV file uploaded by a user, hence the varying table column types. So, in the absence of defined object types, what the application does is to look up the table's column types in a query (Database_Metadata_DBA.getTabDSColumnTypes) and then use the output from this to build a HTML table (Dataset_DBA.getTabDSAsHTMLTable) which is returned to the servlet for displaying in a JSP. Each database operation uses code similar to the example below and these work perfectly normally. But I think what is happening after eight hours of inactivity, is that the system is trying to reuse a connection pool object that MySQL has closed down after its default eight hour period. But I can't seem to resolve this problem and I wonder if anyone can advise? The relevant part of my application's context.xml file is: name = "jdbc/myDataSharer" auth = "Container" maxActive = "100" maxIdle = "30" maxWait = "1" username = "" password = "XX" driverClassName = "com.mysql.jdbc.Driver" url = "jdbc:mysql://XX:/myDataSharer?autoReconnect=true" logAbandoned = "true" minEvictableIdleTimeMillis = "3" numTestsPerEvictionRun = "5" removeAbandoned = "true" removeAbandonedTimeout = "120" testOnBorrow = "false" testOnReturn = "false" testWhileIdle = "true" timeBetweenEvictionRunsMillis = "6" type = "javax.sql.DataSource" validationQuery = "select now()" /> And my application uses servlets which generally have several database IO operations each of the form : public static Dataset getDataset(int DatasetNo) { ConnectionPool_DBA pool = ConnectionPool_DBA.getInstance(); Connection connection = pool.getConnection(); PreparedStatement ps = null; ResultSet rs = null; String query = ("SELECT * " + "FROM Dataset " + "WHERE DatasetNo = ?;"); try { ps = connection.prepareStatement(query); ps.setInt(1, DatasetNo); rs = ps.executeQuery(); if (rs.next()) { Dataset d = new Dataset(); d.setDatasetNo(rs.getInt("DatasetNo")); d.setDatasetName(rs.getString("DatasetName")); ... } return d; } else { return null; } } catch(Exception ex) { logger.error("Error getting Dataset " + DatasetNo + "\n", ex); return null; } finally { DatabaseUtils.closeResultSet(rs); DatabaseUtils.closePreparedStatement(ps); pool.freeConnection(connection); } Where class ConnectionPool_DBA is my own DBCP-based class. Apologies for the length of this message but any help would be much appreciated. Is there a MySQL setting which should be changed at all? Thanks Martin O'Shea. -- View this message in context: http://www.nabble.com/Tomcat-6.X-and-MySQL-connection-pooling-issue-tp25677820p25677820.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For
Re: Tomcat 6.X and MySQL connection pooling issue
Thanks for the reply. Setting testOnBorrow to true is something I'll try but according to: http://commons.apache.org/dbcp/configuration.html If testOnBorrow is set to true, the validation query must be set to a 'non-null string'. Presumably this means that the query must always return a value such as validationQuery = "select now()" always returning the date / time? validationQuery = "select now()" markt-2 wrote: > > MartinOShea wrote: >> But I think what is >> happening after eight hours of inactivity, is that the system is trying >> to >> reuse a connection pool object that MySQL has closed down after its >> default >> eight hour period. > > Sounds likely. > >> testOnBorrow = "false" > > Try setting this to true. > > Mark > > > - > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > > > -- View this message in context: http://www.nabble.com/Tomcat-6.X-and-MySQL-connection-pooling-issue-tp25677820p25679823.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: Tomcat 6.X and MySQL connection pooling issue
So select now() is fine. Thanks. markt-2 wrote: > > MartinOShea wrote: >> Thanks for the reply. >> >> Setting testOnBorrow to true is something I'll try but according to: >> >> http://commons.apache.org/dbcp/configuration.html >> >> If testOnBorrow is set to true, the validation query must be set to a >> 'non-null string'. Presumably this means that the query must always >> return a >> value such as validationQuery = "select now()" always returning the date >> / >> time? > > It just means that testOnBorrow does nothing if validationQuery is set to > "". > For mysql "select 1" is sufficient and saves a call to now(). The Oracle > equivalent would be "select 1 from dual" > > I just checked the dbcp source code and the criteria for the validation > query is > that it must return at least 1 row with no exceptions being thrown. > > Mark > > >> >> validationQuery = "select now()" >> >> markt-2 wrote: >>> MartinOShea wrote: >>>> But I think what is >>>> happening after eight hours of inactivity, is that the system is trying >>>> to >>>> reuse a connection pool object that MySQL has closed down after its >>>> default >>>> eight hour period. >>> Sounds likely. >>> >>>> testOnBorrow = "false" >>> Try setting this to true. >>> >>> Mark >>> >>> >>> - >>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org >>> For additional commands, e-mail: users-h...@tomcat.apache.org >>> >>> >>> >> > > > - > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > > > -- View this message in context: http://www.nabble.com/Tomcat-6.X-and-MySQL-connection-pooling-issue-tp25677820p25680199.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: Tomcat 6.X and MySQL connection pooling issue
The query has been changed to "select 1". Thanks. Pid-6 wrote: > > On 30/09/2009 13:38, MartinOShea wrote: >> >> So select now() is fine. > > (Where "fine" is defined as "not what you were advised to use, for sound > reasons that are require a longer explanation than was necessary at the > time".) > > p > > >> Thanks. >> >> >> markt-2 wrote: >>> >>> MartinOShea wrote: >>>> Thanks for the reply. >>>> >>>> Setting testOnBorrow to true is something I'll try but according to: >>>> >>>> http://commons.apache.org/dbcp/configuration.html >>>> >>>> If testOnBorrow is set to true, the validation query must be set to a >>>> 'non-null string'. Presumably this means that the query must always >>>> return a >>>> value such as validationQuery = "select now()" always returning the >>>> date >>>> / >>>> time? >>> >>> It just means that testOnBorrow does nothing if validationQuery is set >>> to >>> "". >>> For mysql "select 1" is sufficient and saves a call to now(). The Oracle >>> equivalent would be "select 1 from dual" >>> >>> I just checked the dbcp source code and the criteria for the validation >>> query is >>> that it must return at least 1 row with no exceptions being thrown. >>> >>> Mark >>> >>> >>>> >>>> validationQuery = "select now()" >>>> >>>> markt-2 wrote: >>>>> MartinOShea wrote: >>>>>> But I think what is >>>>>> happening after eight hours of inactivity, is that the system is >>>>>> trying >>>>>> to >>>>>> reuse a connection pool object that MySQL has closed down after its >>>>>> default >>>>>> eight hour period. >>>>> Sounds likely. >>>>> >>>>>> testOnBorrow = "false" >>>>> Try setting this to true. >>>>> >>>>> Mark >>>>> >>>>> >>>>> - >>>>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org >>>>> For additional commands, e-mail: users-h...@tomcat.apache.org >>>>> >>>>> >>>>> >>>> >>> >>> >>> - >>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org >>> For additional commands, e-mail: users-h...@tomcat.apache.org >>> >>> >>> >> > > > - > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > > > -- View this message in context: http://www.nabble.com/Tomcat-6.X-and-MySQL-connection-pooling-issue-tp25677820p25680340.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: Tomcat 6.X and MySQL connection pooling issue
Changing testOnBorrow = "true" has not changed the situation with the connection pool failing -can anyone suggest anything? Thanks. Martin. MartinOShea wrote: > > Hello > > I wonder if anyone can advise me on this issue. I have a Tomcat 6.X Java / > JSP application which uses connection pooling to access a MySQL database > but, if the application is left for up to eight hours, one of the pages > fails to display the contents of a dataset upon loading. > > Looking into the logs, I find that I have the following: > > ERROR|29 09 2009|08 42 > 19|http-8080-4|myDataSharer.database_access.Database_Metadata_DBA| - Error > getting types of columns of tabular Dataset 12 > > com.mysql.jdbc.CommunicationsException: Communications link failure due to > underlying exception: > > ** BEGIN NESTED EXCEPTION ** > > java.io.EOFException > > STACKTRACE: > > java.io.EOFException > at com.mysql.jdbc.MysqlIO.readFully(MysqlIO.java:1956) > at com.mysql.jdbc.MysqlIO.reuseAndReadPacket(MysqlIO.java:2368) > at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2867) > at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1616) > at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1708) > at com.mysql.jdbc.Connection.execSQL(Connection.java:3255) > at > com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1293) > at > com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1428) > at > org.apache.tomcat.dbcp.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93) > at > myDataSharer.database_access.Database_Metadata_DBA.getTabDSColumnTypes(Database_Metadata_DBA.java:364) > at > myDataSharer.database_access.Dataset_DBA.getTabDSAsHTMLTable(Dataset_DBA.java:1266) > > And so on for several hundred lines. > > Now the page in question is meant to display data from one of a number of > tables in MySQL which vary in their column types. Each table forms a > dataset which is created from data extracted from RSS feeds or an Excel / > CSV file uploaded by a user, hence the varying table column types. > > So, in the absence of defined object types, what the application does is > to look up the table's column types in a query > (Database_Metadata_DBA.getTabDSColumnTypes) and then use the output from > this to build a HTML table > (Dataset_DBA.getTabDSAsHTMLTable) which is returned to the servlet for > displaying in a JSP. Each database operation uses code similar to the > example below and these work perfectly normally. But I think what is > happening after eight hours of inactivity, is that the system is trying to > reuse a connection pool object that MySQL has closed down after its > default eight hour period. > > But I can't seem to resolve this problem and I wonder if anyone can > advise? > > The relevant part of my application's context.xml file is: > > name = "jdbc/myDataSharer" > auth = "Container" > maxActive = "100" > maxIdle = "30" > maxWait = "1" > username = "" > password = "XX" > driverClassName = "com.mysql.jdbc.Driver" > url = "jdbc:mysql://XX:/myDataSharer?autoReconnect=true" > logAbandoned = "true" > minEvictableIdleTimeMillis = "3" > numTestsPerEvictionRun = "5" > removeAbandoned = "true" > removeAbandonedTimeout = "120" > testOnBorrow = "false" > testOnReturn = "false" > testWhileIdle = "true" > timeBetweenEvictionRunsMillis = "6" > type = "javax.sql.DataSource" > validationQuery = "select now()" /> > > And my application uses servlets which generally have several database IO > operations each of the form : > > public static Dataset getDataset(int DatasetNo) > { > ConnectionPool_DBA pool = ConnectionPool_DBA.getInstance(); > Connection connection = pool.getConnection(); > PreparedStatement ps = null; > ResultSet rs = null; > String query = ("SELECT * " + > "FROM Dataset " + > "WHERE DatasetNo = ?;"); > try { > ps = connection.prepareStatement(query); > ps.setInt(1, DatasetNo); > rs = ps.executeQuery(); > if (rs.next()) > { > Dataset d = new Dataset(); > d.setDatasetNo(rs.getInt("DatasetNo")); > d.setDatasetName(rs.getString("DatasetName"));
Re: Tomcat 6.X and MySQL connection pooling issue
Chris Their is only one connection pool used for the system and that's the one defined in context.xml. Out of interest, I include the code of the connection pool class being used: package myDataSharer.database_access; import java.sql.*; import javax.sql.DataSource; import javax.naming.InitialContext; import org.apache.log4j.Logger; public class ConnectionPool_DBA { static Logger logger = Logger.getLogger(ConnectionPool_DBA.class.getName()); private static ConnectionPool_DBA pool = null; private static DataSource dataSource = null; public synchronized static ConnectionPool_DBA getInstance() { if (pool == null) { pool = new ConnectionPool_DBA(); } return pool; } private ConnectionPool_DBA() { try { InitialContext ic = new InitialContext(); dataSource = (DataSource) ic.lookup("java:/comp/env/jdbc/myDataSharer"); } catch(Exception ex) { logger.error("Error getting a connection pool's datasource\n", ex); } } public void freeConnection(Connection c) { try { c.close(); } catch (Exception ex) { logger.error("Error terminating a connection pool connection\n", ex); } } public Connection getConnection() { try { return dataSource.getConnection(); } catch (Exception ex) { logger.error("Error getting a connection pool connection\n", ex); return null; } } } This should be read in tandem with the code I posted yesterday illustrating a typical database operation. How do I switch query logging on though? Thanks Martin O'Shea. Christopher Schultz-2 wrote: > > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > Martin, > > On 10/1/2009 3:47 AM, MartinOShea wrote: >> Changing testOnBorrow = "true" has not changed the situation with the >> connection pool failing -can anyone suggest anything? > > Are you sure you are configuring the right connection pool? > > Can you turn-on query logging on the server and observe what queries are > being executing when? > > - -chris > -BEGIN PGP SIGNATURE- > Version: GnuPG v1.4.9 (MingW32) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ > > iEYEARECAAYFAkrE04oACgkQ9CaO5/Lv0PBLTwCeMjZGg/2Y14BPwiTjXLvnPhU0 > SY4An2bjLBYUnCCWjEN/dsBnsEd8vIHA > =vhEb > -END PGP SIGNATURE- > > - > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > > > -- View this message in context: http://www.nabble.com/Tomcat-6.X-and-MySQL-connection-pooling-issue-tp25677820p25702523.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org
Re: Tomcat 6.X and MySQL connection pooling issue
Chris Many thanks. I'll be trying these out over the next few days. Martin O'Shea. Christopher Schultz-2 wrote: > > -BEGIN PGP SIGNED MESSAGE- > Hash: SHA1 > > Martin, > > On 10/1/2009 12:53 PM, MartinOShea wrote: >> private static DataSource dataSource = null; > > I wouldn't recommend caching the DataSource object. This limits your > ability to reconfigure the DataSource on the fly if you want to. Local > JNDI lookups are super fast, so caching doesn't really save you that > much time. > >> This should be read in tandem with the code I posted yesterday >> illustrating >> a typical database operation. >> >> How do I switch query logging on though? > > On the server: > http://dev.mysql.com/doc/refman/5.0/en/query-log.html > > On the client: > http://dev.mysql.com/doc/refman/5.0/en/connector-j-reference-configuration-properties.html > > (Look for the profileSQL setting). > > Note that, if you are using DBCP's reconnection capabilities, it's not > necessary to set autoReconnect=true in the connection string (you'll > just end up creating 2 new connections when your connection is dropped, > though the first one will end up being destroyed pretty quickly). > > - -chris > -BEGIN PGP SIGNATURE- > Version: GnuPG v1.4.9 (MingW32) > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/ > > iEYEARECAAYFAkrFIHkACgkQ9CaO5/Lv0PDUcwCfW1Wz6ieZ0pEnm/p+dm9vsiVo > IFIAoIlQSxys4cHzVi+YAro3BDrV0dju > =Ojns > -END PGP SIGNATURE- > > - > To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org > For additional commands, e-mail: users-h...@tomcat.apache.org > > > -- View this message in context: http://www.nabble.com/Tomcat-6.X-and-MySQL-connection-pooling-issue-tp25677820p25707037.html Sent from the Tomcat - User mailing list archive at Nabble.com. - To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org For additional commands, e-mail: users-h...@tomcat.apache.org