io.opentracing.contrib.jdbc.ConnectionInfo Java Examples

The following examples show how to use io.opentracing.contrib.jdbc.ConnectionInfo. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: URLParser.java    From java-jdbc with Apache License 2.0 6 votes vote down vote up
/**
 * parse the url to the ConnectionInfo
 */
public static ConnectionInfo parse(String url) {
  if (null == url) {
    return ConnectionInfo.UNKNOWN_CONNECTION_INFO;
  }
  String lowerCaseUrl = url.toLowerCase();
  ConnectionURLParser parser = findURLParser(lowerCaseUrl);
  if (parser == null) {
    return ConnectionInfo.UNKNOWN_CONNECTION_INFO;
  }
  try {
    return parser.parse(url);
  } catch (Exception e) {
    log.log(Level.WARNING, "error occurs when parsing jdbc url");
  }
  return ConnectionInfo.UNKNOWN_CONNECTION_INFO;
}
 
Example #2
Source File: OracleURLParser.java    From java-jdbc with Apache License 2.0 6 votes vote down vote up
@Override
public ConnectionInfo parse(final String url) {
  if (url != null && (url.startsWith(PREFIX_THIN) || url.startsWith(PREFIX_OCI))) {
    String trimmedURL;
    if (url.startsWith(PREFIX_THIN)) {
      trimmedURL = url.substring(PREFIX_THIN.length());
    } else {
      trimmedURL = url.substring(PREFIX_OCI.length());
    }
    OracleConnectionInfo connectionInfo = parseTnsName(trimmedURL);
    if (connectionInfo == null) {
      connectionInfo = parseEasyConnect(trimmedURL);
    }
    if (connectionInfo != null) {
      return new ConnectionInfo.Builder(connectionInfo.getDbPeer()) //
          .dbType(DB_TYPE) //
          .dbInstance(connectionInfo.getDbInstance()) //
          .build();
    }
  }
  return null;
}
 
Example #3
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsePostgresqlJDBCURLWithHostAndPort() {
  ConnectionInfo connectionInfo = URLParser
      .parser("jdbc:postgresql//primaryhost:3307/test?profileSQL=true");
  assertEquals(POSTGRESQL, connectionInfo.getDbType());
  assertEquals("test", connectionInfo.getDbInstance());
  assertEquals("primaryhost:3307", connectionInfo.getDbPeer());
  assertEquals("test[postgresql(primaryhost:3307)]", connectionInfo.getPeerService());
}
 
Example #4
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseOracleJDBCURLWithUserNameAndPassword() {
  ConnectionInfo connectionInfo = URLParser
      .parser("jdbc:oracle:thin:scott/tiger@myhost:1521:orcl");
  assertEquals(ORACLE, connectionInfo.getDbType());
  assertEquals("orcl", connectionInfo.getDbInstance());
  assertEquals("myhost:1521", connectionInfo.getDbPeer());
  assertEquals("orcl[oracle(myhost:1521)]", connectionInfo.getPeerService());
}
 
Example #5
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseOracleJDBCURLWithSID() {
  ConnectionInfo connectionInfo = URLParser
      .parser("jdbc:oracle:thin:@orcl");
  assertEquals(ORACLE, connectionInfo.getDbType());
  assertEquals("orcl", connectionInfo.getDbInstance());
  assertEquals("orcl:1521", connectionInfo.getDbPeer());
  assertEquals("orcl[oracle(orcl:1521)]", connectionInfo.getPeerService());
}
 
Example #6
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseH2JDBCURLWithEmbedded() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:h2:file:/data/sample");
  assertEquals(H2, connectionInfo.getDbType());
  assertEquals("/data/sample", connectionInfo.getDbInstance());
  assertEquals("localhost:-1", connectionInfo.getDbPeer());
  assertEquals("/data/sample[h2(localhost:-1)]", connectionInfo.getPeerService());
}
 
Example #7
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseH2JDBCURLWithEmbeddedRunningInWindows() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:h2:file:c:/data/sample");
  assertEquals(H2, connectionInfo.getDbType());
  assertEquals("c:/data/sample", connectionInfo.getDbInstance());
  assertEquals("localhost:-1", connectionInfo.getDbPeer());
  assertEquals("c:/data/sample[h2(localhost:-1)]", connectionInfo.getPeerService());
}
 
Example #8
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseH2JDBCURLWithImplicitFile() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:h2:c:/data/sample");
  assertEquals(H2, connectionInfo.getDbType());
  assertEquals("c:/data/sample", connectionInfo.getDbInstance());
  assertEquals("localhost:-1", connectionInfo.getDbPeer());
  assertEquals("c:/data/sample[h2(localhost:-1)]", connectionInfo.getPeerService());
}
 
Example #9
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseH2JDBCURLWithMemoryMode() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:h2:mem:test_mem");

  assertEquals(H2, connectionInfo.getDbType());
  assertEquals("test_mem", connectionInfo.getDbInstance());
  assertEquals("localhost:-1", connectionInfo.getDbPeer());
  assertEquals("test_mem[h2(localhost:-1)]", connectionInfo.getPeerService());
}
 
Example #10
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseH2JDBCURL() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:h2:tcp://localhost:8084/~/sample");
  assertEquals(H2, connectionInfo.getDbType());
  assertEquals("sample", connectionInfo.getDbInstance());
  assertEquals("localhost:8084", connectionInfo.getDbPeer());
  assertEquals("sample[h2(localhost:8084)]", connectionInfo.getPeerService());
}
 
Example #11
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsePostgresqlJDBCURLWithHost() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:postgresql//primaryhost/test");
  assertEquals(POSTGRESQL, connectionInfo.getDbType());
  assertEquals("test", connectionInfo.getDbInstance());
  assertEquals("primaryhost:5432", connectionInfo.getDbPeer());
  assertEquals("test[postgresql(primaryhost:5432)]", connectionInfo.getPeerService());
}
 
Example #12
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseOracleTNSNameWithMultiAddress() {
  ConnectionInfo connectionInfo = URLParser.parse(
      "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL= TCP)(HOST=hostA)(PORT= 1523 ))(ADDRESS=(PROTOCOL=TCP)(HOST=hostB)(PORT= 1521 )))(SOURCE_ROUTE=yes)(CONNECT_DATA=(SERVICE_NAME=orcl)))");
  assertEquals(ORACLE, connectionInfo.getDbType());
  assertEquals("orcl", connectionInfo.getDbInstance());
  assertEquals("hostA:1523,hostB:1521", connectionInfo.getDbPeer());
  assertEquals("orcl[oracle(hostA:1523,hostB:1521)]", connectionInfo.getPeerService());
}
 
Example #13
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsePostgresqlJDBCURLWithMultiHost() {
  ConnectionInfo connectionInfo = URLParser.parse(
      "jdbc:postgresql//primaryhost:3307,secondaryhost1,secondaryhost2/test?profileSQL=true");
  assertEquals(POSTGRESQL, connectionInfo.getDbType());
  assertEquals("test", connectionInfo.getDbInstance());
  assertEquals("primaryhost:3307,secondaryhost1:5432,secondaryhost2:5432",
      connectionInfo.getDbPeer());
  assertEquals("test[postgresql(primaryhost:3307,secondaryhost1:5432,secondaryhost2:5432)]",
      connectionInfo.getPeerService());
}
 
Example #14
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParsePostgresqlJDBCURLWithConnectorJs() {
  ConnectionInfo connectionInfo = URLParser
      .parser("jdbc:postgresql:replication://master,slave1,slave2,slave3/test");
  assertEquals(POSTGRESQL, connectionInfo.getDbType());
  assertEquals("test", connectionInfo.getDbInstance());
  assertEquals("master:5432,slave1:5432,slave2:5432,slave3:5432", connectionInfo.getDbPeer());
  assertEquals("test[postgresql(master:5432,slave1:5432,slave2:5432,slave3:5432)]",
      connectionInfo.getPeerService());
}
 
Example #15
Source File: OracleURLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest(name = "[{0}]")
@MethodSource("easyConnectUrls")
void parseEasyConnect(final String url, final String dbPeer, final String dbInstance) {
  final ConnectionInfo result = testee.parse(url);
  assertThat(result.getDbType()).isEqualTo(ORACLE);
  assertThat(result.getDbPeer()).isEqualTo(dbPeer);
  assertThat(result.getDbInstance()).isEqualTo(dbInstance);
}
 
Example #16
Source File: OracleURLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest(name = "[{0}]")
@MethodSource("tnsNameUrls")
void parseTnsName(final String url, final String dbPeer, final String dbInstance) {
  final ConnectionInfo result = testee.parse(url);
  assertThat(result.getDbType()).isEqualTo(ORACLE);
  assertThat(result.getDbPeer()).isEqualTo(dbPeer);
  assertThat(result.getDbInstance()).isEqualTo(dbInstance);
}
 
Example #17
Source File: OracleURLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@ParameterizedTest(name = "[{0}]")
@MethodSource("otherUrls")
void parseOther(final String url, final String dbPeer, final String dbInstance) {
  final ConnectionInfo result = testee.parse(url);
  assertThat(result.getDbType()).isEqualTo(ORACLE);
  assertThat(result.getDbPeer()).isEqualTo(dbPeer);
  assertThat(result.getDbInstance()).isEqualTo(dbInstance);
}
 
Example #18
Source File: JdbcAspect.java    From java-spring-cloud with Apache License 2.0 5 votes vote down vote up
/**
 * Intercepts calls to {@link DataSource#getConnection()} (and related), wrapping the outcome in a
 * {@link TracingConnection} proxy
 *
 * @param pjp the intercepted join point
 * @return a new {@link TracingConnection} proxy wrapping the result of the joint point
 * @throws Throwable in case of wrong JDBC URL
 */
@Around("execution(java.sql.Connection *.getConnection(..)) && target(javax.sql.DataSource)")
public Object getConnection(final ProceedingJoinPoint pjp) throws Throwable {
  Connection conn = (Connection) pjp.proceed();
  if (WrapperProxy.isWrapper(conn, TracingConnection.class)) {
    return conn;
  }
  String url = conn.getMetaData().getURL();
  ConnectionInfo connectionInfo = URLParser.parse(url);
  return WrapperProxy.wrap(conn, new TracingConnection(conn, connectionInfo,
      withActiveSpanOnly, ignoredStatements, GlobalTracer.get()));
}
 
Example #19
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseOracleTNSName() {
  ConnectionInfo connectionInfo = URLParser.parse(
      "jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST= localhost )(PORT= 1521))(CONNECT_DATA=(SERVER=DEDICATED)(SERVICE_NAME=orcl)))");
  assertEquals(ORACLE, connectionInfo.getDbType());
  assertEquals("orcl", connectionInfo.getDbInstance());
  assertEquals("localhost:1521", connectionInfo.getDbPeer());
  assertEquals("orcl[oracle(localhost:1521)]", connectionInfo.getPeerService());
}
 
Example #20
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseOracleServiceName() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:oracle:thin:@//localhost:1521/orcl");
  assertEquals(ORACLE, connectionInfo.getDbType());
  assertEquals("orcl", connectionInfo.getDbInstance());
  assertEquals("localhost:1521", connectionInfo.getDbPeer());
  assertEquals("orcl[oracle(localhost:1521)]", connectionInfo.getPeerService());
}
 
Example #21
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseOracleJDBCURLWithHostAndPort() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:oracle:thin:@localhost:1522:orcl");
  assertEquals(ORACLE, connectionInfo.getDbType());
  assertEquals("orcl", connectionInfo.getDbInstance());
  assertEquals("localhost:1522", connectionInfo.getDbPeer());
  assertEquals("orcl[oracle(localhost:1522)]", connectionInfo.getPeerService());
}
 
Example #22
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseOracleJDBCURLWithHost() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:oracle:thin:@localhost:orcl");
  assertEquals(ORACLE, connectionInfo.getDbType());
  assertEquals("orcl", connectionInfo.getDbInstance());
  assertEquals("localhost:1521", connectionInfo.getDbPeer());
  assertEquals("orcl[oracle(localhost:1521)]", connectionInfo.getPeerService());
}
 
Example #23
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseMysqlJDBCURLWithConnectorJs() {
  ConnectionInfo connectionInfo = URLParser
      .parser("jdbc:mysql:replication://master,slave1,slave2,slave3/test");
  assertEquals(MYSQL, connectionInfo.getDbType());
  assertEquals("test", connectionInfo.getDbInstance());
  assertEquals("master:3306,slave1:3306,slave2:3306,slave3:3306", connectionInfo.getDbPeer());
  assertEquals("test[mysql(master:3306,slave1:3306,slave2:3306,slave3:3306)]",
      connectionInfo.getPeerService());
}
 
Example #24
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseMysqlJDBCURLWithMultiHost() {
  ConnectionInfo connectionInfo = URLParser
      .parser("jdbc:mysql//primaryhost:3307,secondaryhost1,secondaryhost2/test?profileSQL=true");
  assertEquals(MYSQL, connectionInfo.getDbType());
  assertEquals("test", connectionInfo.getDbInstance());
  assertEquals("primaryhost:3307,secondaryhost1:3306,secondaryhost2:3306",
      connectionInfo.getDbPeer());
  assertEquals("test[mysql(primaryhost:3307,secondaryhost1:3306,secondaryhost2:3306)]",
      connectionInfo.getPeerService());
}
 
Example #25
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseMysqlJDBCURLWithHostAndPort() {
  ConnectionInfo connectionInfo = URLParser
      .parser("jdbc:mysql//primaryhost:3307/test?profileSQL=true");
  assertEquals(MYSQL, connectionInfo.getDbType());
  assertEquals("test", connectionInfo.getDbInstance());
  assertEquals("primaryhost:3307", connectionInfo.getDbPeer());
  assertEquals("test[mysql(primaryhost:3307)]", connectionInfo.getPeerService());
}
 
Example #26
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseMysqlURLWithoutExplicitDBOrQueryString() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:mysql://primaryhost");
  assertEquals(MYSQL, connectionInfo.getDbType());
  assertEquals("", connectionInfo.getDbInstance());
  assertEquals("primaryhost:3306", connectionInfo.getDbPeer());
}
 
Example #27
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseMysqlJDBCURLWithoutDB() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:mysql//primaryhost?profileSQL=true");
  assertEquals(MYSQL, connectionInfo.getDbType());
  assertEquals("", connectionInfo.getDbInstance());
  assertEquals("primaryhost:3306", connectionInfo.getDbPeer());
  assertEquals("mysql(primaryhost:3306)", connectionInfo.getPeerService());
}
 
Example #28
Source File: URLParserTest.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Test
public void testParseMysqlJDBCURLWithHost() {
  ConnectionInfo connectionInfo = URLParser.parse("jdbc:mysql//primaryhost/test");
  assertEquals(MYSQL, connectionInfo.getDbType());
  assertEquals("test", connectionInfo.getDbInstance());
  assertEquals("primaryhost:3306", connectionInfo.getDbPeer());
  assertEquals("test[mysql(primaryhost:3306)]", connectionInfo.getPeerService());
}
 
Example #29
Source File: H2URLParser.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public ConnectionInfo parse(String url) {
  int[] databaseNameRangeIndex = fetchDatabaseNameRangeIndexFromURLForH2FileMode(url);
  if (databaseNameRangeIndex != null) {
    return new ConnectionInfo.Builder(LOCALHOST, -1).dbType(H2_DB_TYPE)
        .dbInstance(fetchDatabaseNameFromURL(url, databaseNameRangeIndex)).build();
  }

  databaseNameRangeIndex = fetchDatabaseNameRangeIndexFromURLForH2MemMode(url);
  if (databaseNameRangeIndex != null) {
    return new ConnectionInfo.Builder(LOCALHOST, -1).dbType(H2_DB_TYPE)
        .dbInstance(fetchDatabaseNameFromURL(url, databaseNameRangeIndex)).build();
  }

  databaseNameRangeIndex = fetchDatabaseNameRangeIndexFromURLForH2ImplicitFileMode(url);
  if (databaseNameRangeIndex != null) {
    return new ConnectionInfo.Builder(LOCALHOST, -1).dbType(H2_DB_TYPE)
        .dbInstance(fetchDatabaseNameFromURL(url, databaseNameRangeIndex)).build();
  }

  String[] hostAndPort = fetchDatabaseHostsFromURL(url).split(":");
  if (hostAndPort.length == 1) {
    return new ConnectionInfo.Builder(hostAndPort[0], DEFAULT_PORT).dbType(H2_DB_TYPE)
        .dbInstance(fetchDatabaseNameFromURL(url)).build();
  } else {
    return new ConnectionInfo.Builder(hostAndPort[0], Integer.valueOf(hostAndPort[1]))
        .dbType(H2_DB_TYPE).dbInstance(fetchDatabaseNameFromURL(url)).build();
  }
}
 
Example #30
Source File: PostgreSQLURLParser.java    From java-jdbc with Apache License 2.0 5 votes vote down vote up
@Override
public ConnectionInfo parse(String url) {
  URLLocation location = fetchDatabaseHostsIndexRange(url);
  String hosts = url.substring(location.startIndex(), location.endIndex());
  String[] hostSegment = hosts.split(",");
  if (hostSegment.length > 1) {
    StringBuilder sb = new StringBuilder();
    for (String host : hostSegment) {
      if (host.split(":").length == 1) {
        sb.append(host + ":" + DEFAULT_PORT + ",");
      } else {
        sb.append(host + ",");
      }
    }
    if (',' == sb.charAt(sb.length() - 1)) {
      sb.deleteCharAt(sb.length() - 1);
    }
    return new ConnectionInfo.Builder(sb.toString()).dbType(DB_TYPE)
        .dbInstance(fetchDatabaseNameFromURL(url)).build();
  } else {
    String[] hostAndPort = hostSegment[0].split(":");
    if (hostAndPort.length != 1) {
      return new ConnectionInfo.Builder(hostAndPort[0], Integer.valueOf(hostAndPort[1]))
          .dbType(DB_TYPE).dbInstance(fetchDatabaseNameFromURL(url)).build();
    } else {
      return new ConnectionInfo.Builder(hostAndPort[0], DEFAULT_PORT).dbType(DB_TYPE)
          .dbInstance(fetchDatabaseNameFromURL(url)).build();
    }
  }
}