Andy
Andy

Reputation: 8908

Persistent Sessions with JDBC and Tomcat

We have a cluster of Tomcat servers that share a common web server running mod_jk. We currently use sticky sessions to take care of session handling, but we would like to move to JDBC session sharing. Does anyone have a good resource or step-by-step solution to deal with this?

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

EDIT:

I think the content of my question must be confusing. The sessions to which I am referring are user sessions (JSESSIONID), not connections to the database. What I want to do is use the database to handle the user sessions so that when one server in the cluster goes down, the transition to another server is seamless to the user. Right now, the user is logged out when an error on the server occurs.

Upvotes: 7

Views: 6680

Answers (2)

mindas
mindas

Reputation: 26713

Most of this is available in Tomcat documentation, see Persistent Manager Implementation.

You can also look at this.

Upvotes: 5

titania424
titania424

Reputation: 457

Since you say JDBC, I'm assuming you mean in Java? Your question seems to have some ambiguity, so I'm not sure this is what you are looking for, but based on my understanding, I'll give it a shot. Anyway, I use connection pooling (Apache commons dbcp) and Spring, which makes it pretty easy.

<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 setup, the connections to the database are pooled and reused. The datasource is managed as a Spring bean, then dependency-injected into where it is used. Spring handled the sharing of the jdbc sessions for you, and voila! Here is how I do the dependency injection with annotations:

private JdbcTemplate jdbcTemplate;

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

Even if you aren't using Spring for MVC or anything else, the Spring JDBC tools are really nice.

Upvotes: 0

Related Questions