尝试一个新的DruidDataSource

git-svn-id: http://code.alibabatech.com/svn/druid/trunk@260 b9813039-fb51-4c41-a8b9-e21c2acb5095
This commit is contained in:
wenshao
2011-06-05 13:30:34 +00:00
parent e7aaa8a66d
commit aaea3928fa
7 changed files with 1639 additions and 28 deletions
@@ -30,7 +30,7 @@ import javax.sql.StatementEventListener;
*/
public final class ConnectionHolder {
private final DruidDataSource dataSource;
private final DruidAbstractDataSource dataSource;
private final Connection conn;
private final List<ConnectionEventListener> connectionEventListeners = new CopyOnWriteArrayList<ConnectionEventListener>();
private final List<StatementEventListener> statementEventListeners = new CopyOnWriteArrayList<StatementEventListener>();
@@ -85,7 +85,7 @@ public final class ConnectionHolder {
return statementPool;
}
public DruidDataSource getDataSource() {
public DruidAbstractDataSource getDataSource() {
return dataSource;
}
@@ -30,6 +30,7 @@ import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.atomic.AtomicLong;
import javax.security.auth.callback.NameCallback;
import javax.security.auth.callback.PasswordCallback;
@@ -726,6 +727,36 @@ public abstract class DruidAbstractDataSource implements DataSource, DataSourceP
return createTimespan / (1000 * 1000);
}
@Override
public Driver getRawDriver() {
return driver;
}
private final AtomicLong connectionIdSeed = new AtomicLong(10000);
private final AtomicLong statementIdSeed = new AtomicLong(20000);
private final AtomicLong resultSetIdSeed = new AtomicLong(50000);
public long createConnectionId() {
return connectionIdSeed.incrementAndGet();
}
public long createStatementId() {
return statementIdSeed.getAndIncrement();
}
public long createResultSetId() {
return resultSetIdSeed.getAndIncrement();
}
void initStatement(Statement stmt) throws SQLException {
if (queryTimeout > 0) {
stmt.setQueryTimeout(queryTimeout);
}
}
protected abstract void recycle(PoolableConnection pooledConnection) throws SQLException;
abstract void incrementCreateCount();
public static class DruidPoolConnectionFactory implements ConnectionFactory {
@@ -18,14 +18,12 @@ package com.alibaba.druid.pool;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
@@ -615,16 +613,6 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
return null;
}
@Override
public Driver getRawDriver() {
return driver;
}
void initStatement(Statement stmt) throws SQLException {
if (queryTimeout > 0) {
stmt.setQueryTimeout(queryTimeout);
}
}
/** Instance key */
protected String instanceKey = null;
@@ -687,19 +675,5 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
return names;
}
private final AtomicLong connectionIdSeed = new AtomicLong(10000);
private final AtomicLong statementIdSeed = new AtomicLong(20000);
private final AtomicLong resultSetIdSeed = new AtomicLong(50000);
public long createConnectionId() {
return connectionIdSeed.incrementAndGet();
}
public long createStatementId() {
return statementIdSeed.getAndIncrement();
}
public long createResultSetId() {
return resultSetIdSeed.getAndIncrement();
}
}
@@ -0,0 +1,35 @@
package com.alibaba.druid.pool;
import java.sql.Connection;
import java.sql.SQLException;
public class DruidDataSource2 extends DruidAbstractDataSource {
private static final long serialVersionUID = 1L;
@Override
public Connection getConnection() throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
public Connection getConnection(String username, String password) throws SQLException {
// TODO Auto-generated method stub
return null;
}
@Override
void incrementCreateCount() {
// TODO Auto-generated method stub
}
@Override
protected void recycle(PoolableConnection pooledConnection) throws SQLException {
// TODO Auto-generated method stub
}
}
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,165 @@
/*
* ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*/
/*
*
*
*
*
*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
* http://creativecommons.org/publicdomain/zero/1.0/
*/
package com.alibaba.druid.util;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
/**
* A {@link BlockingQueue} in which producers may wait for consumers
* to receive elements. A {@code TransferQueue} may be useful for
* example in message passing applications in which producers
* sometimes (using method {@link #transfer}) await receipt of
* elements by consumers invoking {@code take} or {@code poll}, while
* at other times enqueue elements (via method {@code put}) without
* waiting for receipt.
* {@linkplain #tryTransfer(Object) Non-blocking} and
* {@linkplain #tryTransfer(Object,long,TimeUnit) time-out} versions of
* {@code tryTransfer} are also available.
* A {@code TransferQueue} may also be queried, via {@link
* #hasWaitingConsumer}, whether there are any threads waiting for
* items, which is a converse analogy to a {@code peek} operation.
*
* <p>Like other blocking queues, a {@code TransferQueue} may be
* capacity bounded. If so, an attempted transfer operation may
* initially block waiting for available space, and/or subsequently
* block waiting for reception by a consumer. Note that in a queue
* with zero capacity, such as {@link SynchronousQueue}, {@code put}
* and {@code transfer} are effectively synonymous.
*
* <p>This interface is a member of the
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
* Java Collections Framework</a>.
*
* @since 1.7
* @author Doug Lea
* @param <E> the type of elements held in this collection
*/
public interface TransferQueue<E> extends BlockingQueue<E> {
/**
* Transfers the element to a waiting consumer immediately, if possible.
*
* <p>More precisely, transfers the specified element immediately
* if there exists a consumer already waiting to receive it (in
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
* otherwise returning {@code false} without enqueuing the element.
*
* @param e the element to transfer
* @return {@code true} if the element was transferred, else
* {@code false}
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this queue
*/
boolean tryTransfer(E e);
/**
* Transfers the element to a consumer, waiting if necessary to do so.
*
* <p>More precisely, transfers the specified element immediately
* if there exists a consumer already waiting to receive it (in
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
* else waits until the element is received by a consumer.
*
* @param e the element to transfer
* @throws InterruptedException if interrupted while waiting,
* in which case the element is not left enqueued
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this queue
*/
void transfer(E e) throws InterruptedException;
/**
* Transfers the element to a consumer if it is possible to do so
* before the timeout elapses.
*
* <p>More precisely, transfers the specified element immediately
* if there exists a consumer already waiting to receive it (in
* {@link #take} or timed {@link #poll(long,TimeUnit) poll}),
* else waits until the element is received by a consumer,
* returning {@code false} if the specified wait time elapses
* before the element can be transferred.
*
* @param e the element to transfer
* @param timeout how long to wait before giving up, in units of
* {@code unit}
* @param unit a {@code TimeUnit} determining how to interpret the
* {@code timeout} parameter
* @return {@code true} if successful, or {@code false} if
* the specified waiting time elapses before completion,
* in which case the element is not left enqueued
* @throws InterruptedException if interrupted while waiting,
* in which case the element is not left enqueued
* @throws ClassCastException if the class of the specified element
* prevents it from being added to this queue
* @throws NullPointerException if the specified element is null
* @throws IllegalArgumentException if some property of the specified
* element prevents it from being added to this queue
*/
boolean tryTransfer(E e, long timeout, TimeUnit unit)
throws InterruptedException;
/**
* Returns {@code true} if there is at least one consumer waiting
* to receive an element via {@link #take} or
* timed {@link #poll(long,TimeUnit) poll}.
* The return value represents a momentary state of affairs.
*
* @return {@code true} if there is at least one waiting consumer
*/
boolean hasWaitingConsumer();
/**
* Returns an estimate of the number of consumers waiting to
* receive elements via {@link #take} or timed
* {@link #poll(long,TimeUnit) poll}. The return value is an
* approximation of a momentary state of affairs, that may be
* inaccurate if consumers have completed or given up waiting.
* The value may be useful for monitoring and heuristics, but
* not for synchronization control. Implementations of this
* method are likely to be noticeably slower than those for
* {@link #hasWaitingConsumer}.
*
* @return the number of consumers waiting to receive elements
*/
int getWaitingConsumerCount();
}
@@ -0,0 +1,31 @@
package com.alibaba.druid;
import junit.framework.TestCase;
import com.alibaba.druid.util.LinkedTransferQueue;
public class TestLinkedTransferQueue extends TestCase {
public void test_0() throws Exception {
final LinkedTransferQueue<Object> q = new LinkedTransferQueue<Object>();
Thread takeThread = new Thread("Take Thread") {
public void run() {
try {
for (;;) {
q.take();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
takeThread.start();
// q.take();
q.put(1);
q.put(2);
System.out.println(q.size());
}
}