It just takes 15 seconds. Send your
details and we’ll get back to you soon.

Tomcat 6 Session Persistence through JDBCStore

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:

<Manager className='org.apache.catalina.session.PersistentManager'
 saveOnRestart='false' minIdelSwap='0' maxIdleSwap='0'  maxIdleBackup='1'> 
<Store className="org.apache.catalina.session.JDBCStore" 
driverName="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost/tomcat?user=username&amp;password=password" 
sessionTable="sessions" 
sessionIdCol="session_id"
sessionDataCol="session_data"
sessionValidCol="valid_session" 
sessionMaxInactiveCol="max_inactive"
sessionLastAccessedCol="last_access"
sessionAppCol='app_name' />
 </Manager>

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.]

13 Responses to Tomcat 6 Session Persistence through JDBCStore

  1. Mihir says:

    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.

  2. abhishek gupta says:

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

  3. Hesh says:

    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!

  4. Fateh says:

    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

  5. Fateh says:

    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

  6. Anil says:

    Hi
    If I replicate my database , can I give multiple databse names in the connection URL
    Regards
    Anil

  7. Pingback: philosophical take

  8. Prashant says:

    what is ” KEY kapp_name(app_name) ” , i tried with mysql but it is showing error.

  9. Anant Anand Gupta says:

    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.

  10. Shirsangshu Dutta says:

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

  11. 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!!!!

  12. sdutta83 says:

    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!!!!

  13. Shirsangshu says:

    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!!!!

Leave a Reply

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

2 + 2 =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>