Store the session in the DATABASE

478 views
Skip to first unread message

Sathurshan S

unread,
Aug 4, 2021, 10:52:24 AM8/4/21
to WildFly
Hi all,

Currently I need to disable the session replication for prod env but we have to share the session data between the cluster. our solution is use to the database(MSSQL) to store the session. I found couple of blogs in the internet but those are suitable for old wildfly version. what are the configs need to do support the requirement?

Paul Ferraro

unread,
Aug 4, 2021, 1:09:52 PM8/4/21
to WildFly
1. First you'll need to configure a DataSource to manage connections to your database.  See: https://docs.wildfly.org/24/Admin_Guide.html#DataSource  For this example, I will assume pool-name="MSSQL".

2. Configure the Infinispan subsystem with an Infinispan invalidation-cache that uses a shared, persistent jdbc-store that references your DataSource (by its pool-name) from step 1.
e.g.
<cache-container name="web" module="org.wildfly.clustering.web.infinispan">
   <transport/>
   <!-- ... -->
   <invalidation-cache name="foo">
     <jdbc-store data-source="MSSQL" dialect="SQL_SERVER" shared="true" passivation="false" purge="false"/>
   </invalidation-cache>
</cache-container>

3. Configure your web application to use the cache from step 2.  You can achieve this in 1 of 4 ways:

a. Within the Infinispan subsystem, change the default cache of the web cache container to the cache you created in step 2.
e.g.
  <cache-container name="web" default-cache="foo">...</cache-container>

b. Within the distributable-web subsystem, modify the default session-management profile to use the cache defined in step 2.
e.g.
  <subsystem xmlns="urn:jboss:domain:distributable-web:3.0" default-session-management="bar" ...>
    <infinispan-session-management name="bar" cache-container="web" cache="foo" granularity="ATTRIBUTE">
      <primary-owner-affinity/>
    </infinispan-session-management>
    <!-- ... -->
  </subsytem>

c. Within the distributable-web subsystem, create a new session-management profile referencing the cache you created in step 2, and configure your web application to use it.
e.g.
  <subsystem xmlns="urn:jboss:domain:distributable-web:3.0" default-session-management="default" ...>
    <infinispan-session-management name="bar" cache-container="web" cache="foo" granularity="ATTRIBUTE">
      <primary-owner-affinity/>
    </infinispan-session-management>
    <!-- ... -->
  </subsytem>

/WEB-INF/distributable-web.xml:
  <?xml version="1.0" encoding="UTF-8"?>
  <distributable-web xmlns="urn:jboss:distributable-web:2.0">
    <session-management name="bar"/>
  </distributable-web>

d. Configure an application-specific session-mangement profile, configured to use the cache defined in step 2.
e.g.
/WEB-INF/distributable-web.xml:
  <?xml version="1.0" encoding="UTF-8"?>
  <distributable-web xmlns="urn:jboss:distributable-web:2.0">
    <infinispan-session-management cache-container="web" cache="foo" granularity="ATTRIBUTE">
      <primary-owner-affinity/>
    </infinispan-session-management>
  </distributable-web>

4. Make sure your web application is distributable, e.g.
/WEB-INF/web.xml:
  <?xml version="1.0" encoding="UTF-8"?>
  <web-app xmlns="...">
    <distributable/>
    <!--- ... -->
  </web-app>

Let me know if you have additional questions.

Paul

Sathurshan S

unread,
Aug 5, 2021, 3:18:30 AM8/5/21
to WildFly
Hi Paul,

Thanks for the reply. I have tried but I'm getting this error after added this configuration

<invalidation-cache name="foo">
            <jdbc-store data-source="ix3" dialect="SQL_SERVER" fetch-state="false" passivation="false" purge="false" shared="true"/>
</invalidation-cache>

2021-08-05 12:38:41,154 WARN  [org.infinispan.persistence.manager.PersistenceManagerImpl] (expiration-thread--p21-t1) [] ISPN000026: Caught exception purging data container!: org.infinispan.persistence.spi.StoreUnavailableException: Store org.infinispan.persistence.jdbc.stringbased.JdbcStringBasedStore@6f8cf1c0 is unavailable
at org.infinispan.persistence.manager.PersistenceManagerImpl.pollStoreAvailability(PersistenceManagerImpl.java:204)
at org.infinispan.persistence.manager.PersistenceManagerImpl.start(PersistenceManagerImpl.java:169)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_265]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_265]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_265]
at java.lang.reflect.Method.invoke(Method.java:498) [rt.jar:1.8.0_265]
at org.infinispan.commons.util.ReflectionUtil.invokeMethod(ReflectionUtil.java:188) [infinispan-commons-9.4.19.Final-redhat-00001.jar:9.4.19.Final-redhat-00001]

Paul Ferraro

unread,
Aug 16, 2021, 8:53:24 AM8/16/21
to WildFly
This exception implies that Infinispan is unable to connect to the database, i.e. Connection.isValid(...) returned false.  You might want to verify that your JDBC driver implements this method correctly.
Reply all
Reply to author
Forward
0 new messages