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

📄 cacheconnection.java

📁 java编写的数据库连接池的源代码。包含有连接池的源码。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
						s.recycle();
						ss.add(s);
					}
					catch (SQLException sqle)
					{
						s.release();
					}
				}
			}
		}
	}
	
	private String calcHitRate(int hits, int reqs)
	{
		return (reqs == 0) ? "" : (((float)hits / reqs) * 100f) + "% hit rate";
	}

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

	public void setAutoCommit(boolean autoCommit) throws SQLException
	{
		con.setAutoCommit(autoCommit);
	}

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

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

	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.get(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.get(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.get(0)).close();
				}
			}
		}
		else
		{
			flushOpenCallableStatements();
			flushSpareCallableStatements();
		}

		// Put connection back in default state
		if (!getAutoCommit())
		{
			try { rollback(); }
			catch (SQLException sqle) { sqle.printStackTrace(); }
			setAutoCommit(true);
		}
		clearWarnings();
	}

	/**
	 * 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);
	}

	/**
	 * 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();
				ss.clear();
			}
		}
	}

	/**
	 * 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.get(0)).release();
				ssUsed.clear();
			}
		}
	}

	/**
	 * 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.entrySet().iterator(); iter.hasNext();)
				{
					List list = (List)((Map.Entry)iter.next()).getValue();
					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.get(0)).release();
				psUsed.clear();
			}
		}
	}

	/**
	 * 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.entrySet().iterator(); iter.hasNext();)
				{
					List list = (List)((Map.Entry)iter.next()).getValue();
					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.get(0)).release();
				csUsed.clear();
			}
		}
	}

	/**
	 * Flushes the non-cached Statements for this connection.
	 */
	protected void flushNonCachedStatements() throws SQLException
	{
		int count = (nonCached != null) ? nonCached.size() : 0;
		if (count > 0)
		{
			if (pool.isDebug())
				pool.log("Closing " + count + " open non-cached Statement" + (count > 1 ? "s" : ""));
			synchronized(nonCached)
			{
				while (!nonCached.isEmpty())
					((Statement)nonCached.get(0)).close();
				nonCached.clear();
			}
		}
	}

	/**
	 * 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 { flushNonCachedStatements(); }
		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);
		nonCached.add(x);
		return x;
	}

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

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

⌨️ 快捷键说明

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