{"id":1350,"date":"2010-07-21T18:19:22","date_gmt":"2010-07-21T12:49:22","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=1350"},"modified":"2011-07-29T16:40:00","modified_gmt":"2011-07-29T11:10:00","slug":"tomcat-6-session-persistence-through-jdbcstore","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/tomcat-6-session-persistence-through-jdbcstore\/","title":{"rendered":"Tomcat 6 Session Persistence through JDBCStore"},"content":{"rendered":"<p>In one of our recent projects, we needed to save the HTTP session in the database.<br \/>\nThis blog refers the Apache documentation as found here: <a href=\"http:\/\/tomcat.apache.org\/tomcat-6.0-doc\/config\/manager.html\" target=\"_blank\">http:\/\/tomcat.apache.org\/tomcat-6.0-doc\/config\/manager.html<\/a><\/p>\n<p>These are the following steps that need to be followed:<\/p>\n<p>Step 1: Create a database named <strong>tomcat<\/strong> (as shown in our example in Step 3 below) or any other name as specified in the <strong>&#8216;connectionURL&#8217;<\/strong> attribute of the  &lt;Store&gt; element.<\/p>\n<p>Step 2: Create the following table in the newly created database:<\/p>\n<blockquote>\n<pre>create table sessions (\r\nsession_id     varchar(100) not null primary key,\r\nvalid_session  char(1) not null,\r\nmax_inactive   int not null,\r\nlast_access    bigint not null,\r\napp_name       varchar(255),\r\nsession_data   mediumblob,\r\nKEY kapp_name(app_name)\r\n);\r\n<\/pre>\n<\/blockquote>\n<p>Step 3: Copy the <strong>context.xml<\/strong> file available at the global level at this location: <strong>&lt;TOMCAT_HOME&gt;\/conf<\/strong>  to your application&#8217;s <strong>META-INF<\/strong> folder and replace the  &lt;Manager&gt; element with the following:<\/p>\n<blockquote>\n<pre lang=\"groovy\">\r\n<Manager className='org.apache.catalina.session.PersistentManager'\r\n saveOnRestart='false' minIdelSwap='0' maxIdleSwap='0'  maxIdleBackup='1'> \r\n<Store className=\"org.apache.catalina.session.JDBCStore\" \r\ndriverName=\"com.mysql.jdbc.Driver\"\r\nconnectionURL=\"jdbc:mysql:\/\/localhost\/tomcat?user=username&amp;password=password\" \r\nsessionTable=\"sessions\" \r\nsessionIdCol=\"session_id\"\r\nsessionDataCol=\"session_data\"\r\nsessionValidCol=\"valid_session\" \r\nsessionMaxInactiveCol=\"max_inactive\"\r\nsessionLastAccessedCol=\"last_access\"\r\nsessionAppCol='app_name' \/>\r\n <\/Manager>\r\n<\/pre>\n<\/blockquote>\n<p>If these settings are placed in the &#8216;<strong>&lt;TOMCAT_HOME&gt;\/conf\/context.xml<\/strong>&#8216;, 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 &#8220;context.xml&#8221; file in the <strong>&lt;APP_HOME&gt;\/META-INF <\/strong>sub-folder inside the <strong>webapps<\/strong> folder.<br \/>\nPlease note that attributes used in the <strong>&lt;Store&gt;<\/strong> element make use of the table columns as created in the database in Step 2.<\/p>\n<p>Step 4: Set the following system properties named &#8211; &#8216;<strong>org.apache.catalina.session.StandardSession.ACTIVITY_CHECK<\/strong>&#8216; to &#8216;<strong>true<\/strong>&#8216;.<br \/>\nFor further information on this, read the &#8216;Persistent Manager Implementation&#8217; section at this link : <a href=\"http:\/\/tomcat.apache.org\/tomcat-6.0-doc\/config\/manager.html\" target=\"_blank\">http:\/\/tomcat.apache.org\/tomcat-6.0-doc\/config\/manager.html<\/a><\/p>\n<p>Step 5: Make sure you have placed the MySql jar in the <strong>&lt;TOMCAT_HOME&gt;\/lib <\/strong>folder.<\/p>\n<p>Now you can try to hit the application&#8217;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.<\/p>\n<p>Cheers!!!<\/p>\n<p>Abhishek &amp; Imran<\/p>\n<p>abhishek@intelligrape.com | imran@intelligrape.com<\/p>\n<p>[Intelligrape Software Pvt. Ltd.]<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 [&hellip;]<\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":29},"categories":[1],"tags":[108,338,339,594,17],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1350"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=1350"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/1350\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=1350"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=1350"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=1350"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}