Persistent sessions with JDBC and Tomcat

We have a Tomcat server cluster with a shared web server running on mod_jk. We currently use sticky sessions to handle sessions, but we would like to move on to sharing JDBC sessions. Does anyone have a good resource or step-by-step solution to handle this?

I was not sure if this question is for stackoverflow, serverfault or DBA, but here it is. :)

EDIT:

I think the content of my question should be confusing. The sessions I am referring to are user sessions (JSESSIONID), not database connections. I want to use a database to handle user sessions, so when one server in the cluster is lowered, switching to another server is easy for the user. The user is currently logging out when an error occurs on the server.

+6
java tomcat jdbc session
source share
2 answers

Most of this information is available in the Tomcat documentation , see Implementing Persistent Manager.

You can also watch this .

+5
source share

Since you speak JDBC, I assume you mean Java? Your question seems to have some ambiguity, so I'm not sure if this is what you are looking for, but based on my understanding, I will do it. Anyway, I use the connection pool (Apache commons dbcp) and Spring, which makes it pretty simple.

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/databasename"/> <property name="username" value="root"/> <property name="password" value="password"/> 

Then in the code I use Spring jdbctemplate, and with this setting, the database connections are combined and reused. The data source is managed as a Spring bean and then injected into the dependency where it is used. Spring handled the separation of jdbc sessions for you and voilà! Here's how I do dependency injection with annotations:

 private JdbcTemplate jdbcTemplate; @Autowired public void setDataSource(DataSource dataSource) { this.jdbcTemplate = new JdbcTemplate(dataSource); } 

Even if you are not using Spring for MVC or anything else, Spring JDBC tools are really nice.

+1
source share

All Articles