Tomcat 6 Session Persistence through JDBCStore

21 / Jul / 2010 by Abhishek Tejpaul 12 comments

In one of our recent projects, we needed to save the HTTP session in the database.
This blog refers the Apache documentation as found here: http://tomcat.apache.org/tomcat-6.0-doc/config/manager.html

These are the following steps that need to be followed:

Step 1: Create a database named tomcat (as shown in our example in Step 3 below) or any other name as specified in the ‘connectionURL’ attribute of the <Store> element.

Step 2: Create the following table in the newly created database:

create table sessions (
session_id     varchar(100) not null primary key,
valid_session  char(1) not null,
max_inactive   int not null,
last_access    bigint not null,
app_name       varchar(255),
session_data   mediumblob,
KEY kapp_name(app_name)
);

Step 3: Copy the context.xml file available at the global level at this location: <TOMCAT_HOME>/conf to your application’s META-INF folder and replace the <Manager> element with the following:

 

 

If these settings are placed in the ‘<TOMCAT_HOME>/conf/context.xml‘, it will have a global effect on all the applications running on the server. For an application specific setting, you can place a newly created “context.xml” file in the <APP_HOME>/META-INF sub-folder inside the webapps folder.
Please note that attributes used in the <Store> element make use of the table columns as created in the database in Step 2.

Step 4: Set the following system properties named – ‘org.apache.catalina.session.StandardSession.ACTIVITY_CHECK‘ to ‘true‘.
For further information on this, read the ‘Persistent Manager Implementation’ section at this link : http://tomcat.apache.org/tomcat-6.0-doc/config/manager.html

Step 5: Make sure you have placed the MySql jar in the <TOMCAT_HOME>/lib folder.

Now you can try to hit the application’s URL and check the database table to see the newly persisted session. Please note that it takes around 60 seconds to see the stored session in the database so you might have to wait a bit. You can have multiple instances of tomcat running your application pointing to the same database and can share this persisted session in case any of the tomcat instance crashes.

Cheers!!!

Abhishek & Imran

abhishek@intelligrape.com | imran@intelligrape.com

[Intelligrape Software Pvt. Ltd.]

FOUND THIS USEFUL? SHARE IT

comments (12)

  1. Shirsangshu

    set “CATALINA_OPTS=%CATALINA_OPTS% -Dfile.encoding=UTF8 -Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=true -Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true -Duser.timezone=GMT -Xmx1024m -XX:MaxPermSize=256m” ..add these lines in the $CATALINA_HOME\bin\setenv.bat if you are using windows and setenv.sh in linux.Create one if you dont have setenv.bat Follow the steps mentioned above .It worked for me Thanks!!!!

    Reply
  2. sdutta83

    set “CATALINA_OPTS=%CATALINA_OPTS% -Dfile.encoding=UTF8 -Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=true -Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true -Duser.timezone=GMT -Xmx1024m -XX:MaxPermSize=256m” ..add these lines in the $CATALINA_HOME\bin\setenv.bat if you are using windows and setenv.sh in linux.Create one if you dont have setenv.bat Follow the steps mentioned above .It worked for me Thanks!!!!

    Reply
  3. Shirsangshu Dutta

    set “CATALINA_OPTS=%CATALINA_OPTS% -Dfile.encoding=UTF8 -Dorg.apache.catalina.STRICT_SERVLET_COMPLIANCE=true -Dorg.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true -Duser.timezone=GMT -Xmx1024m -XX:MaxPermSize=256m” ..add these lines in the $CATALINA_HOMEbinsetenv.bat if you are using windows and setenv.sh in linux.Create one if you dont have setenv.bat Follow the steps mentioned above .It worked for me Thanks!!!!

    Reply
  4. Shirsangshu Dutta

    @anant
    driverClassName=”net.sourceforge.jtds.jdbc.Driver”
    url=”jdbc:jtds:sqlserver://L-156019768:1433/your _db_name”

    Reply
  5. Anant Anand Gupta

    can you please help me out achiving the same for the SQL Server in place of mySQL. I have downloaded the JDBC driver jar from microsoft and have placed it in the tomcat lib. i am struggling to find the class name and the drivername parameters property for the above configuration.

    Reply
  6. Fateh

    hi all
    i run liferay portal on tomcat-6.0.26
    i did the exactly when i run my server i get this log out

    Mar 14, 2012 2:50:57 PM org.apache.catalina.session.JDBCStore getConnection
    INFO: The database connection is null or was found to be closed. Trying to re-open it.

    this is my
    #context.xml

    #server.xml

    and i add in my
    #system-ext.properties
    org.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true

    Reply
  7. Fateh

    hi all
    i run liferay portal on tomcat-6.0.26
    i did the exactly when i run my server i get this log out

    Mar 14, 2012 2:50:57 PM org.apache.catalina.session.JDBCStore getConnection
    INFO: The database connection is null or was found to be closed. Trying to re-open it.

    this is my
    #context.xml

    #server.xml

    <!– Added by me Wolf
    –>

    <!–

    –>


    <!–

    –>

    <!–

    –>

    <!– You should set jvmRoute to support load-balancing via AJP ie :

    –>

    <!–

    –>

    <!–

    –>

    <!–

    –>

    <!–

    –>

    and i add in my
    #system-ext.properties

    org.apache.catalina.session.StandardSession.ACTIVITY_CHECK=true

    Reply
  8. Hesh

    Hi Abhishek,

    how can I change the time interval to see the stored session in the database? (which you have mentioned as 60 seconds in step 5)

    Cheers!

    Reply
  9. abhishek gupta

    I found this link very useful. Whatever written, I exactly followed….now successfully setup the environment….cheers!

    Reply
  10. Mihir

    I did exactly as you wrote above in article. I appreciate it very much . But I am getting error that my application is not available.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *