下面列出了java.sql.Connection#toString ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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) {
}
}
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) {
}
}
/**
* 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));
}
@Test
public void shouldNotFailCallingToStringOverAnInvalidConnection() throws Exception {
PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
Connection c = ds.getConnection();
c.close();
c.toString();
}
@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;
}
@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;
}
/**
* 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);
}
}
/**
* 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);
}
}
/**
* 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);
}
/**
* 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));
}
/**
* 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);
}
/**
* 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));
}
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);
}
@Test
public void shouldNotFailCallingToStringOverAnInvalidConnection() throws Exception {
PooledDataSource ds = createPooledDataSource(JPETSTORE_PROPERTIES);
Connection c = ds.getConnection();
c.close();
c.toString();
}
/**
* 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);
}
}
/**
* 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));
}
/**
* 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);
}
/**
* 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));
}
/**
* 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);
}
/**
* 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));
}