⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cacheconnection.java

📁 DBPool,一个很好的数据库连接池的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
	public void rollback() throws SQLException
	{
		con.rollback();
	}

	/**
	 * Puts connection back in a state where it can be reused.
	 */
	public void recycle() throws SQLException
	{
		// Close all open Statements
		if (cacheS)
		{
			int count = (ssUsed != null) ? ssUsed.size() : 0;
			if (count > 0)
			{
				if (pool.isDebug())
					pool.log("Cleaning " + count + " cached Statement" + (count > 1 ? "s" : ""));
				synchronized(ssUsed)
				{
					while (!ssUsed.isEmpty())
						((Statement)ssUsed.remove(0)).close();
				}
			}
		}
		else
		{
			flushOpenStatements();
			flushSpareStatements();
		}

		// Close all open PreparedStatements
		if (cacheP)
		{
			int count = (psUsed != null) ? psUsed.size() : 0;
			if (count > 0)
			{
				if (pool.isDebug())
					pool.log("Cleaning " + count + " cached PreparedStatement" + (count > 1 ? "s" : ""));
				synchronized(psUsed)
				{
					while (!psUsed.isEmpty())
						((CachedPreparedStatement)psUsed.remove(0)).close();
				}
			}
		}
		else
		{
			flushOpenPreparedStatements();
			flushSparePreparedStatements();
		}

		// Close all open CallableStatements
		if (cacheC)
		{
			int count = (csUsed != null) ? csUsed.size() : 0;
			if (count > 0)
			{
				if (pool.isDebug())
					pool.log("Cleaning " + count + " cached CallableStatement" + (count > 1 ? "s" : ""));
				synchronized(csUsed)
				{
					while (!csUsed.isEmpty())
						((CachedCallableStatement)csUsed.remove(0)).close();
				}
			}
		}
		else
		{
			flushOpenCallableStatements();
			flushSpareCallableStatements();
		}

		// Close all open non-cachable PreparedStatements.
		flushOpenNonCachableStatements();

		// Put connection back in default state
		if (!getAutoCommit())
		{
			try { rollback(); }
			catch (SQLException sqle) { pool.log(sqle); }
			setAutoCommit(true);
		}
		clearWarnings();
		
		// Clear type map entries.
		Map tm = getTypeMap();
		if (tm != null)
			tm.clear();
	}

	/**
	 * Overrides method to provide caching support.
	 */
	public void close() throws SQLException
	{
		if (!open)
			throw new SQLException("Connection already closed");
		open = false;
		// Hand itself back to the pool
		pool.freeConnection(this);
	}

	/**
	 * Returns the current number of spare Statements that are cached.
	 */
	public int getSpareStatementCount()
	{
		return ss.size();
	}

	/**
	 * Returns the current number of Statements that are in use
	 * (not including PreparedStatements & CallableStatements).
	 */
	public int getOpenStatementCount()
	{
		return ssUsed.size();
	}

	/**
	 * Returns the current number of spare PreparedStatements that are cached.
	 */
	public int getSparePreparedStatementCount()
	{
		int count = 0;
		synchronized(ps)
		{
			for (Iterator it = ps.values().iterator(); it.hasNext();)
				count += ((List)it.next()).size();
		}
		return count;
	}

	/**
	 * Returns the current number of PreparedStatements that are in use
	 * (not including CallableStatements).
	 */
	public int getOpenPreparedStatementCount()
	{
		return psUsed.size();
	}

	/**
	 * Returns the current number of spare CallableStatements that are cached.
	 */
	public int getSpareCallableStatementCount()
	{
		int count = 0;
		synchronized(cs)
		{
			for (Iterator it = cs.values().iterator(); it.hasNext();)
				count += ((List)it.next()).size();
		}
		return count;
	}

	/**
	 * Returns the current number of CallableStatements that are in use.
	 */
	public int getOpenCallableStatementCount()
	{
		return csUsed.size();
	}

	/**
	 * Returns the current number of non-cachable statements that are in use.
	 * (Currently only some PreparedStatements are non-cachable by virtue of
	 * a request made at creation for support for auto-generated keys.)
	 * @see snaq.db.CacheConnection#prepareStatement(String, int)
	 * @see snaq.db.CacheConnection#prepareStatement(String, int[])
	 * @see snaq.db.CacheConnection#prepareStatement(String, String[])
	 */
	public int getOpenNonCachableStatementCount()
	{
		return nonCachable.size();
	}

	/**
	 * Flushes the spare Statement caches for this connection.
	 */
	protected void flushSpareStatements() throws SQLException
	{
		// Close all cached Statements
		int count = (ss != null) ? ss.size() : 0;
		if (count > 0)
		{
			if (pool.isDebug())
				pool.log("Closing " + count + " cached Statement" + (count > 1 ? "s" : ""));
			synchronized(ss)
			{
				while (!ss.isEmpty())
					((CachedStatement)ss.remove(0)).release();
			}
		}
	}

	/**
	 * Flushes the open Statement cache for this connection.
	 */
	protected void flushOpenStatements() throws SQLException
	{
		// Close all open Statements
		int count = (ssUsed != null) ? ssUsed.size() : 0;
		if (count > 0)
		{
			if (pool.isDebug())
				pool.log("Closing " + count + " open Statement" + (count > 1 ? "s" : ""));
			synchronized(ssUsed)
			{
				while (!ssUsed.isEmpty())
					((CachedStatement)ssUsed.remove(0)).release();
			}
		}
	}

	/**
	 * Flushes the spare PreparedStatement cache for this connection.
	 */
	protected void flushSparePreparedStatements() throws SQLException
	{
		// Close all cached PreparedStatements
		int count = (ps != null) ? ps.size() : 0;
		if (count > 0)
		{
			if (pool.isDebug())
				pool.log("Closing " + count + " cached PreparedStatement" + (count > 1 ? "s" : ""));
			synchronized(ps)
			{
				for (Iterator iter = ps.values().iterator(); iter.hasNext();)
				{
					List list = (List)iter.next();
					for (Iterator it = list.iterator(); it.hasNext();)
						((CachedPreparedStatement)it.next()).release();
				}
				ps.clear();
			}
		}
	}

	/**
	 * Flushes the open PreparedStatement cache for this connection.
	 */
	protected void flushOpenPreparedStatements() throws SQLException
	{
		// Close all open PreparedStatements
		int count = (psUsed != null) ? psUsed.size() : 0;
		if (count > 0)
		{
			if (pool.isDebug())
				pool.log("Closing " + count + " open PreparedStatement" + (count > 1 ? "s" : ""));
			synchronized(psUsed)
			{
				while (!psUsed.isEmpty())
					((CachedPreparedStatement)psUsed.remove(0)).release();
			}
		}
	}

	/**
	 * Flushes the spare CallableStatement cache for this connection.
	 */
	protected void flushSpareCallableStatements() throws SQLException
	{
		// Close all cached CallableStatements
		int count = (cs != null) ? cs.size() : 0;
		if (count > 0)
		{
			if (pool.isDebug())
				pool.log("Closing " + count + " cached CallableStatement" + (count > 1 ? "s" : ""));
			synchronized(cs)
			{
				for (Iterator iter = cs.values().iterator(); iter.hasNext();)
				{
					List list = (List)iter.next();
					for (Iterator it = list.iterator(); it.hasNext();)
						((CachedCallableStatement)it.next()).release();
				}
				cs.clear();
			}
		}
	}

	/**
	 * Flushes the open CallableStatement cache for this connection.
	 */
	protected void flushOpenCallableStatements() throws SQLException
	{
		// Close all open CallableStatements
		int count = (csUsed != null) ? csUsed.size() : 0;
		if (count > 0)
		{
			if (pool.isDebug())
				pool.log("Closing " + count + " open CallableStatement" + (count > 1 ? "s" : ""));
			synchronized(csUsed)
			{
				while (!csUsed.isEmpty())
					((CachedCallableStatement)csUsed.remove(0)).release();
			}
		}
	}

	/**
	 * Flushes the non-cachable Statements for this connection.
	 */
	protected void flushOpenNonCachableStatements() throws SQLException
	{
		int count = (nonCachable != null) ? nonCachable.size() : 0;
		if (count > 0)
		{
			if (pool.isDebug())
				pool.log("Closing " + count + " open non-cachable Statement" + (count > 1 ? "s" : ""));
			synchronized(nonCachable)
			{
				while (!nonCachable.isEmpty())
				{
					try { ((Statement)nonCachable.remove(0)).close(); }
					catch (SQLException sqle) { pool.log(sqle); }
				}
			}
		}
	}

	/**
	 * Destroys the wrapped connection.
	 */
	public void release() throws SQLException
	{
		open = false;
		ArrayList list = new ArrayList();

		try { flushSpareStatements(); flushOpenStatements(); }
		catch (SQLException e) { list.add(e); }
		try { flushSparePreparedStatements(); flushOpenPreparedStatements(); }
		catch (SQLException e) { list.add(e); }
		try { flushSpareCallableStatements(); flushOpenCallableStatements(); }
		catch (SQLException e) { list.add(e); }
		try { flushOpenNonCachableStatements(); }
		catch (SQLException e) { list.add(e); }

		try { con.close(); }
		catch (SQLException e) { list.add(e); }

		if (!list.isEmpty())
		{
			SQLException sqle = new SQLException("Problem releasing connection resources");
			for (Iterator it = list.iterator(); it.hasNext();)
			{
				SQLException x = (SQLException)it.next();
				sqle.setNextException(x);
				sqle = x;
			}
			throw sqle;
		}
	}

	public boolean isClosed() throws SQLException
	{
		return con.isClosed();
	}

	public DatabaseMetaData getMetaData() throws SQLException
	{
		return con.getMetaData();
	}

	public void setReadOnly(boolean readOnly) throws SQLException
	{
		con.setReadOnly(readOnly);
	}

	public boolean isReadOnly() throws SQLException
	{
		return con.isReadOnly();
	}

	public void setCatalog(String catalog) throws SQLException
	{
		con.setCatalog(catalog);
	}

	public String getCatalog() throws SQLException
	{
		return con.getCatalog();
	}

	public void setTransactionIsolation(int level) throws SQLException
	{
		con.setTransactionIsolation(level);
	}

	public int getTransactionIsolation() throws SQLException
	{
		return con.getTransactionIsolation();
	}

	public SQLWarning getWarnings() throws SQLException
	{
		return con.getWarnings();
	}

	public void clearWarnings() throws SQLException
	{
		con.clearWarnings();
	}

	public Map getTypeMap() throws SQLException
	{
		return con.getTypeMap();
	}

	public void setTypeMap(Map map) throws SQLException
	{
		con.setTypeMap(map);
	}

	//**********************************
	// Interface methods from JDBC 3.0
	//**********************************

	public void setHoldability(int holdability) throws SQLException
	{
		con.setHoldability(holdability);
	}

	public int getHoldability() throws SQLException
	{
		return con.getHoldability();
	}

	public Savepoint setSavepoint() throws SQLException
	{
		return con.setSavepoint();
	}

	public Savepoint setSavepoint(String name) throws SQLException
	{
		return con.setSavepoint(name);
	}

	public void rollback(Savepoint savepoint) throws SQLException
	{
		con.rollback(savepoint);
	}

	public void releaseSavepoint(Savepoint savepoint) throws SQLException
	{
		con.releaseSavepoint(savepoint);
	}

	public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException
	{
		PreparedStatement x = con.prepareStatement(sql, autoGeneratedKeys);
		nonCachable.add(x);
		return x;
	}

	public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException
	{
		PreparedStatement x = con.prepareStatement(sql, columnIndexes);
		nonCachable.add(x);
		return x;
	}

	public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException
	{
		PreparedStatement x = con.prepareStatement(sql, columnNames);
		nonCachable.add(x);
		return x;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -