📄 jdbcstore.java
字号:
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.catalina.session;
import org.apache.catalina.Container;
import org.apache.catalina.LifecycleException;
import org.apache.catalina.Loader;
import org.apache.catalina.Session;
import org.apache.catalina.Store;
import org.apache.catalina.util.CustomObjectInputStream;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Properties;
/**
* Implementation of the <code>Store</code> interface that stores
* serialized session objects in a database. Sessions that are
* saved are still subject to being expired based on inactivity.
*
* @author Bip Thelin
* @version $Revision: 467222 $, $Date: 2006-10-24 05:17:11 +0200 (mar., 24 oct. 2006) $
*/
public class JDBCStore
extends StoreBase implements Store {
/**
* The descriptive information about this implementation.
*/
protected static String info = "JDBCStore/1.0";
/**
* Context name associated with this Store
*/
private String name = null;
/**
* Name to register for this Store, used for logging.
*/
protected static String storeName = "JDBCStore";
/**
* Name to register for the background thread.
*/
protected String threadName = "JDBCStore";
/**
* The connection username to use when trying to connect to the database.
*/
protected String connectionName = null;
/**
* The connection URL to use when trying to connect to the database.
*/
protected String connectionPassword = null;
/**
* Connection string to use when connecting to the DB.
*/
protected String connectionURL = null;
/**
* The database connection.
*/
private Connection dbConnection = null;
/**
* Instance of the JDBC Driver class we use as a connection factory.
*/
protected Driver driver = null;
/**
* Driver to use.
*/
protected String driverName = null;
// ------------------------------------------------------------- Table & cols
/**
* Table to use.
*/
protected String sessionTable = "tomcat$sessions";
/**
* Column to use for /Engine/Host/Context name
*/
protected String sessionAppCol = "app";
/**
* Id column to use.
*/
protected String sessionIdCol = "id";
/**
* Data column to use.
*/
protected String sessionDataCol = "data";
/**
* Is Valid column to use.
*/
protected String sessionValidCol = "valid";
/**
* Max Inactive column to use.
*/
protected String sessionMaxInactiveCol = "maxinactive";
/**
* Last Accessed column to use.
*/
protected String sessionLastAccessedCol = "lastaccess";
// ------------------------------------------------------------- SQL Variables
/**
* Variable to hold the <code>getSize()</code> prepared statement.
*/
protected PreparedStatement preparedSizeSql = null;
/**
* Variable to hold the <code>keys()</code> prepared statement.
*/
protected PreparedStatement preparedKeysSql = null;
/**
* Variable to hold the <code>save()</code> prepared statement.
*/
protected PreparedStatement preparedSaveSql = null;
/**
* Variable to hold the <code>clear()</code> prepared statement.
*/
protected PreparedStatement preparedClearSql = null;
/**
* Variable to hold the <code>remove()</code> prepared statement.
*/
protected PreparedStatement preparedRemoveSql = null;
/**
* Variable to hold the <code>load()</code> prepared statement.
*/
protected PreparedStatement preparedLoadSql = null;
// ------------------------------------------------------------- Properties
/**
* Return the info for this Store.
*/
public String getInfo() {
return (info);
}
/**
* Return the name for this instance (built from container name)
*/
public String getName() {
if (name == null) {
Container container = manager.getContainer();
String contextName = container.getName();
String hostName = "";
String engineName = "";
if (container.getParent() != null) {
Container host = container.getParent();
hostName = host.getName();
if (host.getParent() != null) {
engineName = host.getParent().getName();
}
}
name = "/" + engineName + "/" + hostName + contextName;
}
return name;
}
/**
* Return the thread name for this Store.
*/
public String getThreadName() {
return (threadName);
}
/**
* Return the name for this Store, used for logging.
*/
public String getStoreName() {
return (storeName);
}
/**
* Set the driver for this Store.
*
* @param driverName The new driver
*/
public void setDriverName(String driverName) {
String oldDriverName = this.driverName;
this.driverName = driverName;
support.firePropertyChange("driverName",
oldDriverName,
this.driverName);
this.driverName = driverName;
}
/**
* Return the driver for this Store.
*/
public String getDriverName() {
return (this.driverName);
}
/**
* Return the username to use to connect to the database.
*
*/
public String getConnectionName() {
return connectionName;
}
/**
* Set the username to use to connect to the database.
*
* @param connectionName Username
*/
public void setConnectionName(String connectionName) {
this.connectionName = connectionName;
}
/**
* Return the password to use to connect to the database.
*
*/
public String getConnectionPassword() {
return connectionPassword;
}
/**
* Set the password to use to connect to the database.
*
* @param connectionPassword User password
*/
public void setConnectionPassword(String connectionPassword) {
this.connectionPassword = connectionPassword;
}
/**
* Set the Connection URL for this Store.
*
* @param connectionURL The new Connection URL
*/
public void setConnectionURL(String connectionURL) {
String oldConnString = this.connectionURL;
this.connectionURL = connectionURL;
support.firePropertyChange("connectionURL",
oldConnString,
this.connectionURL);
}
/**
* Return the Connection URL for this Store.
*/
public String getConnectionURL() {
return (this.connectionURL);
}
/**
* Set the table for this Store.
*
* @param sessionTable The new table
*/
public void setSessionTable(String sessionTable) {
String oldSessionTable = this.sessionTable;
this.sessionTable = sessionTable;
support.firePropertyChange("sessionTable",
oldSessionTable,
this.sessionTable);
}
/**
* Return the table for this Store.
*/
public String getSessionTable() {
return (this.sessionTable);
}
/**
* Set the App column for the table.
*
* @param sessionAppCol the column name
*/
public void setSessionAppCol(String sessionAppCol) {
String oldSessionAppCol = this.sessionAppCol;
this.sessionAppCol = sessionAppCol;
support.firePropertyChange("sessionAppCol",
oldSessionAppCol,
this.sessionAppCol);
}
/**
* Return the web application name column for the table.
*/
public String getSessionAppCol() {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -