Java Code Examples for java.sql.Connection#toString()

The following examples show how to use java.sql.Connection#toString() . These examples are extracted from open source projects. 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 want to check out the right sidebar which shows the related API usage.
Example 1
Source Project: das   File: AbstractConnectionListener.java    License: Apache License 2.0 6 votes vote down vote up
private void putConnectionUrlToCache(Connection connection) {
    if (connection == null) {
        return;
    }

    try {
        String connectionId = connection.toString();
        String url = simpleUrl(connection.getMetaData().getURL());
        if (url == null || url.isEmpty()) {
            return;
        }

        connectionUrlCache.put(connectionId, url);
        LOGGER.info(String.format("%s put to url cache.", connectionId));
    } catch (Throwable e) {
    }
}
 
Example 2
Source Project: das   File: AbstractConnectionListener.java    License: Apache License 2.0 5 votes vote down vote up
private void removeConnectionUrlFromCache(Connection connection) {
    if (connection == null) {
        return;
    }

    try {
        String connectionId = connection.toString();
        connectionUrlCache.remove(connectionId);
        LOGGER.info(String.format("%s removed from url cache.", connectionId));
    } catch (Throwable e) {
    }
}
 
Example 3
/**
 * Check the format of the connection string.  This is the default test
 * to run if this is not a BrokeredConnection class
 */
private static void assertStringFormat(Connection conn) //throws Exception
{
    assertStringPrefix(conn);
    String str = conn.toString(); 
    // matches is not a supported method with JSR169
    if (!JDBC.vmSupportsJSR169())
        assertTrue("\nexpected format:\n " + CONNSTRING_FORMAT + "\nactual value:\n " + str,
                str.matches(CONNSTRING_FORMAT));
}
 
Example 4
Source Project: mybatis   File: PooledDataSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotFailCallingToStringOverAnInvalidConnection() throws Exception {
  PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
  Connection c = ds.getConnection();
  c.close();
  c.toString();
}
 
Example 5
@Override
public Map<String, ?> createMap(Connection con) throws SQLException {
	Map<String, Object> inParms = new HashMap<>();
	String testValue = con.toString();
	inParms.put("in", testValue);
	return inParms;
}
 
Example 6
@Override
public Map<String, ?> createMap(Connection con) throws SQLException {
	Map<String, Object> inParms = new HashMap<String, Object>();
	String testValue = con.toString();
	inParms.put("in", testValue);
	return inParms;
}
 
Example 7
Source Project: gemfirexd-oss   File: JdbcSupport.java    License: Apache License 2.0 5 votes vote down vote up
/**
     * Returns a (new) JDBC connection from the data source.
     * 
     * @return The connection
     */
    public Connection borrowConnection() throws DatabaseOperationException
    {
        try
        {
            Connection connection = null;

            if (_username == null)
            {
                connection = getDataSource().getConnection();
            }
            else
            {
                connection = getDataSource().getConnection(_username, _password);
            }
            if (_log.isDebugEnabled())
            {
                String connName = connection.toString();

                _log.debug("Borrowed connection "+connName+" from data source");
                _openConnectionNames.add(connName);
            }
// GemStone changes BEGIN
            // set the isolation level if required
            if (_isolationLevel >= 0 &&
                connection.getTransactionIsolation() != _isolationLevel) {
              connection.setTransactionIsolation(_isolationLevel);
            }
// GemStone changes END
            return connection;
        }
        catch (SQLException ex)
        {
            throw new DatabaseOperationException("Could not get a connection from the datasource", ex);
        }
    }
 
Example 8
Source Project: gemfirexd-oss   File: JdbcSupport.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Closes the given JDBC connection (returns it back to the pool if the datasource is poolable).
 * 
 * @param connection The connection
 */
public void returnConnection(Connection connection)
{
    try
    {
        if ((connection != null) && !connection.isClosed())
        {
            if (_log.isDebugEnabled())
            {
                String connName = connection.toString();

                _openConnectionNames.remove(connName);

                StringBuilder logMsg = new StringBuilder();

                logMsg.append("Returning connection ");
                logMsg.append(connName);
                logMsg.append(" to data source.\nRemaining connections:");
                if (_openConnectionNames.isEmpty())
                {
                    logMsg.append(" None");
                }
                else
                {
                    for (Iterator it = _openConnectionNames.iterator(); it.hasNext();)
                    {
                      logMsg.append("\n    ");
                      logMsg.append(it.next().toString());
                    }
                }
                _log.debug(logMsg.toString());
            }
            connection.close();
        }
    }
    catch (Exception e)
    {
        _log.warn("Caught exception while returning connection to pool", e);
    }
}
 
Example 9
Source Project: gemfirexd-oss   File: DataSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Make sure this connection's string is unique (DERBY-243)
 */
private static void assertToString(Connection conn) throws Exception
{
    assertStringFormat(conn);
    String str = conn.toString();

    if ( conns.containsKey(str))
    {
        throw new Exception("ERROR: Connection toString() is not unique: "
                + str);
    }
    conns.put(str, conn);
}
 
Example 10
Source Project: gemfirexd-oss   File: DataSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Check the format of the connection string.  This is the default test
 * to run if this is not a BrokeredConnection class
 */
private static void assertStringFormat(Connection conn) //throws Exception
{
    assertStringPrefix(conn);
    String str = conn.toString();
    // matches is not a supported method with JSR169
    if (!JDBC.vmSupportsJSR169())
        assertTrue("\nexpected format:\n " + CONNSTRING_FORMAT + "\nactual value:\n " + str,
                str.matches(CONNSTRING_FORMAT));
}
 
Example 11
/**
 * Make sure this connection's string is unique (DERBY-243)
 */
private static void assertToString(Connection conn) throws Exception
{
    assertStringFormat(conn);
    String str = conn.toString();

    if ( conns.containsKey(str))
    {
        throw new Exception("ERROR: Connection toString() is not unique: " 
                + str);
    }
    conns.put(str, conn);
}
 
Example 12
/**
 * Check the format of the connection string.  This is the default test
 * to run if this is not a BrokeredConnection class
 */
private static void assertStringFormat(Connection conn) //throws Exception
{
    assertStringPrefix(conn);
    String str = conn.toString(); 
    assertTrue("\nexpected format:\n " + CONNSTRING_FORMAT + "\nactual value:\n " + str,
        str.matches(CONNSTRING_FORMAT));
}
 
Example 13
Source Project: dal   File: DalTransaction.java    License: Apache License 2.0 5 votes vote down vote up
private String getRollbackOnlyMessage(int level) {
    String dbName = logicDbName == null ? "" : logicDbName;
    String connectionId = "";
    if (connHolder != null) {
        Connection con = connHolder.getConn();
        if (con != null) {
            connectionId = con.toString();
        }
    }

    return String.format("LogicDbName:%s, Level:%s, ConnectionId:%s", dbName, level, connectionId);
}
 
Example 14
Source Project: mybaties   File: PooledDataSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void shouldNotFailCallingToStringOverAnInvalidConnection() throws Exception {
  PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
  Connection c = ds.getConnection();
  c.close();
  c.toString();
}
 
Example 15
Source Project: gemfirexd-oss   File: JdbcSupport.java    License: Apache License 2.0 5 votes vote down vote up
/**
     * Returns a (new) JDBC connection from the data source.
     * 
     * @return The connection
     */
    public Connection borrowConnection() throws DatabaseOperationException
    {
        try
        {
            Connection connection = null;

            if (_username == null)
            {
                connection = getDataSource().getConnection();
            }
            else
            {
                connection = getDataSource().getConnection(_username, _password);
            }
            if (_log.isDebugEnabled())
            {
                String connName = connection.toString();

                _log.debug("Borrowed connection "+connName+" from data source");
                _openConnectionNames.add(connName);
            }
// GemStone changes BEGIN
            // set the isolation level if required
            if (_isolationLevel >= 0 &&
                connection.getTransactionIsolation() != _isolationLevel) {
              connection.setTransactionIsolation(_isolationLevel);
            }
// GemStone changes END
            return connection;
        }
        catch (SQLException ex)
        {
            throw new DatabaseOperationException("Could not get a connection from the datasource", ex);
        }
    }
 
Example 16
/**
 * Check the format of the connection string.  This is the default test
 * to run if this is not a BrokeredConnection class
 */
private static void assertStringFormat(Connection conn) //throws Exception
{
    assertStringPrefix(conn);
    String str = conn.toString();
    assertTrue("\nexpected format:\n " + CONNSTRING_FORMAT + "\nactual value:\n " + str,
            str.matches(CONNSTRING_FORMAT));
}
 
Example 17
Source Project: gemfirexd-oss   File: DataSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Make sure this connection's string is unique (DERBY-243)
 */
private static void assertToString(Connection conn) throws Exception
{
    assertStringFormat(conn);
    String str = conn.toString();

    if ( conns.containsKey(str))
    {
        throw new Exception("ERROR: Connection toString() is not unique: "
                + str);
    }
    conns.put(str, conn);
}
 
Example 18
Source Project: gemfirexd-oss   File: DataSourceTest.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Check the format of the connection string.  This is the default test
 * to run if this is not a BrokeredConnection class
 */
private static void assertStringFormat(Connection conn) //throws Exception
{
    assertStringPrefix(conn);
    String str = conn.toString();
    // matches is not a supported method with JSR169
    if (!JDBC.vmSupportsJSR169())
        assertTrue("\nexpected format:\n " + CONNSTRING_FORMAT + "\nactual value:\n " + str,
                str.matches(CONNSTRING_FORMAT));
}
 
Example 19
/**
 * Make sure this connection's string is unique (DERBY-243)
 */
private static void assertToString(Connection conn) throws Exception
{
    assertStringFormat(conn);
    String str = conn.toString();

    if ( conns.containsKey(str))
    {
        throw new Exception("ERROR: Connection toString() is not unique: " 
                + str);
    }
    conns.put(str, conn);
}
 
Example 20
/**
 * Check the format of the connection string.  This is the default test
 * to run if this is not a BrokeredConnection class
 */
private static void assertStringFormat(Connection conn) //throws Exception
{
    assertStringPrefix(conn);
    String str = conn.toString(); 
    assertTrue("\nexpected format:\n " + CONNSTRING_FORMAT + "\nactual value:\n " + str,
        str.matches(CONNSTRING_FORMAT));
}