📄 llmdatasource.java
字号:
public SimplePooledConnection(Connection connection, LlmDataSource dataSource) {
this.hashCode = connection.hashCode();
this.realConnection = connection;
this.dataSource = dataSource;
this.createdTimestamp = System.currentTimeMillis();
this.lastUsedTimestamp = System.currentTimeMillis();
this.valid = true;
proxyConnection = (Connection) Proxy.newProxyInstance(Connection.class.getClassLoader(), IFACES, this);
}
/**
* Invalidates the connection
*/
public void invalidate() {
valid = false;
}
/**
* Method to see if the connection is usable
*
* @return True if the connection is usable
*/
public boolean isValid() {
return valid && realConnection != null && dataSource.pingConnection(this);
}
/**
* Getter for the *real* connection that this wraps
* @return The connection
*/
public Connection getRealConnection() {
return realConnection;
}
/**
* Getter for the proxy for the connection
* @return The proxy
*/
public Connection getProxyConnection() {
return proxyConnection;
}
/**
* Gets the hashcode of the real connection (or 0 if it is null)
*
* @return The hashcode of the real connection (or 0 if it is null)
*/
public int getRealHashCode() {
if (realConnection == null) {
return 0;
} else {
return realConnection.hashCode();
}
}
/**
* Getter for the connection type (based on url + user + password)
* @return The connection type
*/
public int getConnectionTypeCode() {
return connectionTypeCode;
}
/**
* Setter for the connection type
* @param connectionTypeCode - the connection type
*/
public void setConnectionTypeCode(int connectionTypeCode) {
this.connectionTypeCode = connectionTypeCode;
}
/**
* Getter for the time that the connection was created
* @return The creation timestamp
*/
public long getCreatedTimestamp() {
return createdTimestamp;
}
/**
* Setter for the time that the connection was created
* @param createdTimestamp - the timestamp
*/
public void setCreatedTimestamp(long createdTimestamp) {
this.createdTimestamp = createdTimestamp;
}
/**
* Getter for the time that the connection was last used
* @return - the timestamp
*/
public long getLastUsedTimestamp() {
return lastUsedTimestamp;
}
/**
* Setter for the time that the connection was last used
* @param lastUsedTimestamp - the timestamp
*/
public void setLastUsedTimestamp(long lastUsedTimestamp) {
this.lastUsedTimestamp = lastUsedTimestamp;
}
/**
* Getter for the time since this connection was last used
* @return - the time since the last use
*/
public long getTimeElapsedSinceLastUse() {
return System.currentTimeMillis() - lastUsedTimestamp;
}
/**
* Getter for the age of the connection
* @return the age
*/
public long getAge() {
return System.currentTimeMillis() - createdTimestamp;
}
/**
* Getter for the timestamp that this connection was checked out
* @return the timestamp
*/
public long getCheckoutTimestamp() {
return checkoutTimestamp;
}
/**
* Setter for the timestamp that this connection was checked out
* @param timestamp the timestamp
*/
public void setCheckoutTimestamp(long timestamp) {
this.checkoutTimestamp = timestamp;
}
/**
* Getter for the time that this connection has been checked out
* @return the time
*/
public long getCheckoutTime() {
return System.currentTimeMillis() - checkoutTimestamp;
}
private Connection getValidConnection() {
if (!valid) {
throw new NestedRuntimeException("Error accessing SimplePooledConnection. Connection has been invalidated (probably released back to the pool).");
}
return realConnection;
}
public int hashCode() {
return hashCode;
}
/**
* Allows comparing this connection to another
*
* @param obj - the other connection to test for equality
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (obj instanceof SimplePooledConnection) {
return realConnection.hashCode() == (((SimplePooledConnection) obj).realConnection.hashCode());
} else if (obj instanceof Connection) {
return hashCode == obj.hashCode();
} else {
return false;
}
}
// **********************************
// Implemented Connection Methods -- Now handled by proxy
// **********************************
/**
* Required for InvocationHandler inplementaion.
*
* @param proxy - not used
* @param method - the method to be executed
* @param args - the parameters to be passed to the method
* @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
*/
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
String methodName = method.getName();
if (CLOSE.hashCode() == methodName.hashCode() && CLOSE.equals(methodName)) {
dataSource.pushConnection(this);
return null;
} else {
try {
return method.invoke(getValidConnection(), args);
} catch (Throwable t) {
throw ClassInfo.unwrapThrowable(t);
}
}
}
public Statement createStatement() throws SQLException {
return getValidConnection().createStatement();
}
public PreparedStatement prepareStatement(String sql) throws SQLException {
return getValidConnection().prepareStatement(sql);
}
public CallableStatement prepareCall(String sql) throws SQLException {
return getValidConnection().prepareCall(sql);
}
public String nativeSQL(String sql) throws SQLException {
return getValidConnection().nativeSQL(sql);
}
public void setAutoCommit(boolean autoCommit) throws SQLException {
getValidConnection().setAutoCommit(autoCommit);
}
public boolean getAutoCommit() throws SQLException {
return getValidConnection().getAutoCommit();
}
public void commit() throws SQLException {
getValidConnection().commit();
}
public void rollback() throws SQLException {
getValidConnection().rollback();
}
public void close() throws SQLException {
dataSource.pushConnection(this);
}
public boolean isClosed() throws SQLException {
return getValidConnection().isClosed();
}
public DatabaseMetaData getMetaData() throws SQLException {
return getValidConnection().getMetaData();
}
public void setReadOnly(boolean readOnly) throws SQLException {
getValidConnection().setReadOnly(readOnly);
}
public boolean isReadOnly() throws SQLException {
return getValidConnection().isReadOnly();
}
public void setCatalog(String catalog) throws SQLException {
getValidConnection().setCatalog(catalog);
}
public String getCatalog() throws SQLException {
return getValidConnection().getCatalog();
}
public void setTransactionIsolation(int level) throws SQLException {
getValidConnection().setTransactionIsolation(level);
}
public int getTransactionIsolation() throws SQLException {
return getValidConnection().getTransactionIsolation();
}
public SQLWarning getWarnings() throws SQLException {
return getValidConnection().getWarnings();
}
public void clearWarnings() throws SQLException {
getValidConnection().clearWarnings();
}
public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
return getValidConnection().createStatement(resultSetType, resultSetConcurrency);
}
public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return getValidConnection().prepareCall(sql, resultSetType, resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
return getValidConnection().prepareCall(sql, resultSetType, resultSetConcurrency);
}
public Map getTypeMap() throws SQLException {
return getValidConnection().getTypeMap();
}
public void setTypeMap(Map map) throws SQLException {
getValidConnection().setTypeMap(map);
}
// **********************************
// JDK 1.4 JDBC 3.0 Methods below
// **********************************
public void setHoldability(int holdability) throws SQLException {
getValidConnection().setHoldability(holdability);
}
public int getHoldability() throws SQLException {
return getValidConnection().getHoldability();
}
public Savepoint setSavepoint() throws SQLException {
return getValidConnection().setSavepoint();
}
public Savepoint setSavepoint(String name) throws SQLException {
return getValidConnection().setSavepoint(name);
}
public void rollback(Savepoint savepoint) throws SQLException {
getValidConnection().rollback(savepoint);
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException {
getValidConnection().releaseSavepoint(savepoint);
}
public Statement createStatement(int resultSetType, int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
return getValidConnection().createStatement(resultSetType, resultSetConcurrency, resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency, int resultSetHoldability)
throws SQLException {
return getValidConnection().prepareStatement(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws SQLException {
return getValidConnection().prepareCall(sql, resultSetType, resultSetConcurrency, resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException {
return getValidConnection().prepareStatement(sql, autoGeneratedKeys);
}
public PreparedStatement prepareStatement(String sql, int columnIndexes[])
throws SQLException {
return getValidConnection().prepareStatement(sql, columnIndexes);
}
public PreparedStatement prepareStatement(String sql, String columnNames[])
throws SQLException {
return getValidConnection().prepareStatement(sql, columnNames);
}
}
public Reference getReference() throws NamingException {
// Reference ref = new Reference(
// LlmDataSource.class.getName(),
// new StringRefAddr("sn",surname),
// EmployeeFactory.class.getName(),
// null
// );
Reference ref = new Reference( LlmDataSource.class.getName() );
return ref;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -