java.sql.Wrapper.unwrap()方法的使用及代码示例

x33g5p2x  于2022-02-03 转载在 其他  
字(8.2k)|赞(0)|评价(0)|浏览(395)

本文整理了Java中java.sql.Wrapper.unwrap()方法的一些代码示例,展示了Wrapper.unwrap()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Wrapper.unwrap()方法的具体详情如下:
包路径:java.sql.Wrapper
类名称:Wrapper
方法名:unwrap

Wrapper.unwrap介绍

[英]Returns an object that implements the given interface. If the caller is not a wrapper, a SQLException will be thrown.
[中]返回实现给定接口的对象。如果调用方不是包装器,将抛出SQLException。

代码示例

代码示例来源:origin: org.springframework.boot/spring-boot

private static <S> S safeUnwrap(Wrapper wrapper, Class<S> target) {
  try {
    return wrapper.unwrap(target);
  }
  catch (Exception ex) {
    return null;
  }
}

代码示例来源:origin: alibaba/druid

@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
  if (null == wrapper) {
    //Best to log error.
    return null;
  }
  if (iface == null) {
    return null;
  }
  if (iface == wrapper.getClass()) {
    return (T) wrapper;
  }
  if (iface == this.getClass()) {
    return (T) this;
  }
  
  if (!(wrapper instanceof WrapperProxy)) {
    if (iface.isInstance(wrapper)) {
      return (T) wrapper;
    }
  }
  return wrapper.unwrap(iface);
}

代码示例来源:origin: alibaba/druid

@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(Wrapper wrapper, Class<T> iface) throws SQLException {
  if (this.pos < filterSize) {
    return nextFilter()
      .unwrap(this, wrapper, iface);
  }
  if (iface == null) {
    return null;
  }
  // if driver is for jdbc 3.0
  if (iface.isAssignableFrom(wrapper.getClass())) {
    return (T) wrapper;
  }
  return wrapper.unwrap(iface);
}

代码示例来源:origin: alibaba/canal

if (conn instanceof java.sql.Wrapper) {
  Class<?> connIface = Class.forName("com.mysql.jdbc.Connection");
  conn = ((java.sql.Wrapper) conn).unwrap(connIface);
  continue;

代码示例来源:origin: codingapi/tx-lcn

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
 final Object result;
 if (iface.isAssignableFrom(getClass())) {
  // if the proxy directly implements the interface or extends it, return the proxy
  result = this;
 } else if (iface.isAssignableFrom(delegate.getClass())) {
  // if the proxied object directly implements the interface or extends it, return
  // the proxied object
  result = unwrapP6SpyProxy();
 } else if (Wrapper.class.isAssignableFrom(delegate.getClass())) {
  // if the proxied object implements the wrapper interface, then
  // return the result of it's unwrap method.
  result = ((Wrapper) unwrapP6SpyProxy()).unwrap(iface);
 } else {
  /*
    This line of code can only be reached when the underlying object does not implement the wrapper
    interface.  This would mean that either the JDBC driver or the wrapper of the underlying object
    does not implement the JDBC 4.0 API.
   */
  throw new SQLException("Can not unwrap to " + iface.getName());
 }
 return iface.cast(result);
}

代码示例来源:origin: com.alibaba/druid

@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
  if (null == wrapper) {
    //Best to log error.
    return null;
  }
  if (iface == null) {
    return null;
  }
  if (iface == wrapper.getClass()) {
    return (T) wrapper;
  }
  if (iface == this.getClass()) {
    return (T) this;
  }
  
  if (!(wrapper instanceof WrapperProxy)) {
    if (iface.isInstance(wrapper)) {
      return (T) wrapper;
    }
  }
  return wrapper.unwrap(iface);
}

代码示例来源:origin: com.alibaba/druid

@SuppressWarnings("unchecked")
@Override
public <T> T unwrap(Wrapper wrapper, Class<T> iface) throws SQLException {
  if (this.pos < filterSize) {
    return nextFilter()
      .unwrap(this, wrapper, iface);
  }
  if (iface == null) {
    return null;
  }
  // if driver is for jdbc 3.0
  if (iface.isAssignableFrom(wrapper.getClass())) {
    return (T) wrapper;
  }
  return wrapper.unwrap(iface);
}

代码示例来源:origin: p6spy/p6spy

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
 final Object result;
 if (iface.isAssignableFrom(getClass())) {
  // if the proxy directly implements the interface or extends it, return the proxy
  result = this;
 } else if (iface.isAssignableFrom(delegate.getClass())) {
  // if the proxied object directly implements the interface or extends it, return
  // the proxied object
  result = unwrapP6SpyProxy();
 } else if (Wrapper.class.isAssignableFrom(delegate.getClass())) {
  // if the proxied object implements the wrapper interface, then
  // return the result of it's unwrap method.
  result = ((Wrapper) unwrapP6SpyProxy()).unwrap(iface);
 } else {
  /*
    This line of code can only be reached when the underlying object does not implement the wrapper
    interface.  This would mean that either the JDBC driver or the wrapper of the underlying object
    does not implement the JDBC 4.0 API.
   */
  throw new SQLException("Can not unwrap to " + iface.getName());
 }
 return iface.cast(result);
}

代码示例来源:origin: geotools/geotools

Wrapper w = cx;
if (w.isWrapperFor(OracleConnection.class)) {
  return w.unwrap(OracleConnection.class);

代码示例来源:origin: stackoverflow.com

try (final Connection poolConn = DataSourceConnectionPool.getConnection()) {
  final Connection conn = poolConn.unwrap(OracleConnection.class);
  //do stuff with conn
  //do not close conn!!
}
//poolConn is returned to the pool

代码示例来源:origin: stackoverflow.com

session.doWork(
  new Work() {
    public void execute(Connection connection) throws SQLException 
    { 
      CopyManager cm = connection.unwrap(org.postgresql.copy.CopyManager.class);
      ... do stuff ...
    }
  }
);

代码示例来源:origin: stackoverflow.com

import java.sql.Connection;
import java.sql.DriverManager;
import org.olap4j.OlapConnection;

Class.forName("mondrian.olap4j.MondrianOlap4jDriver");
Connection conn = DriverManager.getConnection(
  "jdbc:mondrian:" + 
  "Jdbc=jdbc:h2:~/test; " +
  "JdbcUser=sa; " +
  "JdbcPassword=; " +
  "Catalog=file:/mondrian/demo/FoodMart.xml; " +
  "JdbcDrivers=org.h2.Driver");
OlapConnection olapConn = conn.unwrap(OlapConnection.class);

代码示例来源:origin: com.sqlapp/sqlapp-core

@SuppressWarnings("unchecked")
@Override
public <S> S unwrap(Class<S> iface) throws SQLException {
  if (iface.isAssignableFrom(this.getClass())) {
    return (S) this;
  }
  return nativeObject.unwrap(iface);
}

代码示例来源:origin: kumuluz/kumuluzee

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
  if (xaDataSource == null) {
    throw new SQLException("The underlying XADataSource is invalid or cannot be found");
  } else if (iface.isInstance(xaDataSource)) {
    return iface.cast(xaDataSource);
  } else if (xaDataSource instanceof Wrapper) {
    return ((java.sql.Wrapper) xaDataSource).unwrap(iface);
  } else {
    throw new SQLException("The requested interface cannot be unwrapped");
  }
}

代码示例来源:origin: com.kumuluz.ee/kumuluzee-common

@Override
public <T> T unwrap(Class<T> iface) throws SQLException {
  if (xaDataSource == null) {
    throw new SQLException("The underlying XADataSource is invalid or cannot be found");
  } else if (iface.isInstance(xaDataSource)) {
    return iface.cast(xaDataSource);
  } else if (xaDataSource instanceof Wrapper) {
    return ((java.sql.Wrapper) xaDataSource).unwrap(iface);
  } else {
    throw new SQLException("The requested interface cannot be unwrapped");
  }
}

代码示例来源:origin: org.javasimon/javasimon-jdbc41

public <T> T unwrap(Class<T> iface) throws SQLException {
    if (delegateType.equals(iface)) {
      return delegate.isWrapperFor(iface) ? delegate.unwrap(iface) : iface.cast(delegate);
    } else {
      return delegate.unwrap(iface);
    }
  }
}

代码示例来源:origin: stackoverflow.com

//Extract the native JDBC connection from WSJdbcConnection
   WebSphereNativeJdbcExtractor connection = new WebSphereNativeJdbcExtractor();
   //Getting the native connection
   Connection con=connection.getNativeConnection(jdbcTemplate.getDataSource().getConnection());
   ArrayDescriptor desc = ArrayDescriptor.createDescriptor(PROC_IN_PARAM_ALIAS,
       con.unwrap(OracleConnection.class));

代码示例来源:origin: stackoverflow.com

Context ic = new InitialContext();
DataSource ds = (DataSource)ic.lookup("jdbc/OracleDS");
Connection conn = ds.getConnection();

// Returns true if this either implements the interface argument
// or is directly or indirectly a wrapper for an object that does.
if (conn.isWrapperFor(oracle.jdbc.OracleConnection.class)) {
  // Returns an object that implements the given interface to
  // allow access to non-standard methods, or standard methods
  // not exposed by the proxy.
  oracle.jdbc.OracleConnection oraCon = conn.unwrap(oracle.jdbc.OracleConnection.class);
  // Do some Oracle-specific work here.
}

代码示例来源:origin: stackoverflow.com

try (Connection hikariCon = dbConnect.getConnection()) {
 if (hikariCon.isWrapperFor(OracleConnection.class)) {
  OracleConnection connection = hikariCon.unwrap(OracleConnection.class);
  :
  :
 }

代码示例来源:origin: com.github.gquintana.metrics/metrics-sql

protected Object unwrap(MethodInvocation<T> methodInvocation) throws SQLException {
  final Class iface = getClassArg(methodInvocation);
  final Wrapper delegateWrapper = (Wrapper) delegate;
  Object result;
  if (isDelegateType(iface)) {
    result = delegateWrapper.isWrapperFor(iface) ? delegateWrapper.unwrap(iface) : iface.cast(delegateWrapper);
  } else {
    result = delegateWrapper.unwrap(iface);
  }
  return result;
}

相关文章