📄 cmsconnection.java
字号:
/*
* File : $Source: /usr/local/cvs/opencms/src/com/opencms/dbpool/CmsConnection.java,v $
* Date : $Date: 2002/04/03 10:36:30 $
* Version: $Revision: 1.6.4.1 $
*
* This library is part of OpenCms -
* the Open Source Content Mananagement System
*
* Copyright (C) 2001 The OpenCms Group
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* For further information about OpenCms, please see the
* OpenCms Website: http://www.opencms.org
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.opencms.dbpool;
import java.sql.*;
import java.util.*;
import source.org.apache.java.util.*;
/**
* This class is used to create an connection-pool for opencms.
*
* @author a.schouten
*/
public class CmsConnection implements Connection {
/**
* Constant: key for simnple statements
*/
private static final String C_SIMPLE_STATEMENT_KEY = "SIMPLE_STATEMENT";
/**
* The original connection to the db
*/
private Connection m_originalConnection;
/**
* The hook to the pool
*/
private CmsPool m_pool;
/**
* Determines, if the connection is closed already
*/
private boolean m_isClosed = true;
/**
* The time, when this connection was used the last time.
*/
private long m_lastUsed;
/**
* The time, when this connection was established.
*/
private long m_establishTime;
/**
* A pool with all created different statements during the lifetime of
* this connection.
*/
private Hashtable m_statementPool = new Hashtable();
/**
* Constructs a new connection.
*/
CmsConnection(Connection originalConnection, CmsPool pool) {
this(originalConnection, pool, new Hashtable(), System.currentTimeMillis());
}
/**
* Reconstructs a connection.
*/
CmsConnection(Connection originalConnection, CmsPool pool, Hashtable statementPool, long establishTime) {
m_originalConnection = originalConnection;
m_pool = pool;
// create an empty statement
m_lastUsed = System.currentTimeMillis();
m_establishTime = establishTime;
m_statementPool = statementPool;
m_isClosed = false;
}
/**
* Returns the time (ms), when this connection was last used.
*/
long getLastUsed() {
return m_lastUsed;
}
/**
* Returns the time (ms), when this connection was established.
*/
long getEstablishedTime() {
return m_establishTime;
}
/**
* If the connection was closed (and put back into the pool)
* this method throws an exception.
*/
private void checkIsClosed() throws SQLException {
if(m_isClosed) {
throw new SQLException("Connection was already closed.");
}
}
public String nativeSQL(String sql) throws SQLException {
checkIsClosed();
return m_originalConnection.nativeSQL(sql);
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
checkIsClosed();
m_originalConnection.setAutoCommit(autoCommit);
}
public boolean getAutoCommit() throws SQLException {
checkIsClosed();
return m_originalConnection.getAutoCommit();
}
public void commit() throws SQLException {
checkIsClosed();
m_originalConnection.commit();
}
public void rollback() throws SQLException {
checkIsClosed();
m_originalConnection.rollback();
}
public void rollback(java.sql.Savepoint savepoint) throws java.sql.SQLException {
checkIsClosed();
m_originalConnection.rollback(savepoint);
}
public DatabaseMetaData getMetaData() throws SQLException {
checkIsClosed();
return m_originalConnection.getMetaData();
}
public void setReadOnly(boolean readOnly) throws SQLException {
checkIsClosed();
m_originalConnection.setReadOnly(readOnly);
}
public boolean isReadOnly() throws SQLException {
checkIsClosed();
return m_originalConnection.isReadOnly();
}
public void setCatalog(String catalog) throws SQLException {
checkIsClosed();
m_originalConnection.setCatalog(catalog);
}
public String getCatalog() throws SQLException {
checkIsClosed();
return m_originalConnection.getCatalog();
}
public void setTransactionIsolation(int level) throws SQLException {
checkIsClosed();
m_originalConnection.setTransactionIsolation(level);
}
public int getTransactionIsolation() throws SQLException {
checkIsClosed();
return m_originalConnection.getTransactionIsolation();
}
public SQLWarning getWarnings() throws SQLException {
checkIsClosed();
return m_originalConnection.getWarnings();
}
public void clearWarnings() throws SQLException {
checkIsClosed();
m_originalConnection.clearWarnings();
}
public void setTypeMap(Map map) throws SQLException {
checkIsClosed();
m_originalConnection.setTypeMap(map);
}
public Map getTypeMap() throws SQLException {
checkIsClosed();
return m_originalConnection.getTypeMap();
}
/**
* Creates a new CmsStatement from the pool.
*/
public Statement createStatement() throws SQLException {
checkIsClosed();
String key = C_SIMPLE_STATEMENT_KEY;
if(!m_statementPool.containsKey(key)) {
// the pool contains not this statement - create it
m_statementPool.put(key,
new CmsStatement(m_originalConnection.createStatement(), this));
}
// return the simple statement
return (Statement) m_statementPool.get(key);
}
/**
* Creates a new CmsStatement from the pool.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -