下面列出了java.sql.Connection#clearWarnings ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
Print information about the SQL warnings for the connection
to the given PrintWriter.
Walks the list of exceptions, if any.
@param out the place to write to
@param theConnection the connection that may have warnings.
*/
static public void ShowWarnings(PrintWriter out, Connection theConnection) {
try {
// GET CONNECTION WARNINGS
SQLWarning warning = null;
if (theConnection != null) {
ShowWarnings(out, theConnection.getWarnings());
}
if (theConnection != null) {
theConnection.clearWarnings();
}
} catch (SQLException e) {
ShowSQLException(out, e);
}
}
/**
* @return a Connection instance that can be used to connect to the
* given database, if a previously-opened connection is available in
* the cache. Returns null if none is available in the map.
*/
public synchronized Connection getConnection(String connectStr,
String username) throws SQLException {
CacheKey key = new CacheKey(connectStr, username);
Connection cached = connectionMap.get(key);
if (null != cached) {
connectionMap.remove(key);
if (cached.isReadOnly()) {
// Read-only mode? Don't want it.
cached.close();
}
if (cached.isClosed()) {
// This connection isn't usable.
return null;
}
cached.rollback(); // Reset any transaction state.
cached.clearWarnings();
LOG.debug("Got cached connection for " + key);
}
return cached;
}
/**
Print information about the SQL warnings for the connection
to the given PrintWriter.
Walks the list of exceptions, if any.
@param out the place to write to
@param theConnection the connection that may have warnings.
*/
static public void ShowWarnings(PrintWriter out, Connection theConnection) {
try {
// GET CONNECTION WARNINGS
SQLWarning warning = null;
if (theConnection != null) {
ShowWarnings(out, theConnection.getWarnings());
}
if (theConnection != null) {
theConnection.clearWarnings();
}
} catch (SQLException e) {
ShowSQLException(out, e);
}
}
/**
* Sakai modification: don't set autocommit, don't rollback if so configured!
*/
public void passivateObject(Object obj) throws Exception
{
if (obj instanceof Connection)
{
Connection conn = (Connection) obj;
if (m_rollbackOnReturn)
{
if (!conn.getAutoCommit() && !conn.isReadOnly())
{
Exception e = new RuntimeException("Automatic Transaction Rollback");
log.error("Transaction RolledBack!", e);
conn.rollback();
}
}
conn.clearWarnings();
// conn.setAutoCommit(true);
}
if (obj instanceof DelegatingConnection)
{
((DelegatingConnection) obj).passivate();
}
}
/**
* Positive tests for forward only cursors.
*
* This method tests forward only cursors.
*
*
* @exception SQLException
* Thrown if some unexpected error happens
*/
public void testForwardOnlyPositive() throws SQLException {
Connection conn = getConnection();
ResultSet rs;
Statement s_f_r = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
// We should have gotten no warnings and a read only forward only cursor
JDBC.assertNoWarnings(conn.getWarnings());
conn.clearWarnings();
// Verify that setMaxRows(4) succeeds
s_f_r.setMaxRows(5);
assertEquals(5, s_f_r.getMaxRows());
rs = s_f_r.executeQuery("values 1, 2, 3, 4, 5, 6");
// Iterate straight thru RS, expect only 5 rows.
JDBC.assertDrainResults(rs, 5);
s_f_r.close();
}
/**
Print information about the SQL warnings for the connection
to the given PrintWriter.
Walks the list of exceptions, if any.
@param out the place to write to
@param theConnection the connection that may have warnings.
*/
static public void ShowWarnings(PrintWriter out, Connection theConnection) {
try {
// GET CONNECTION WARNINGS
SQLWarning warning = null;
if (theConnection != null) {
ShowWarnings(out, theConnection.getWarnings());
}
if (theConnection != null) {
theConnection.clearWarnings();
}
} catch (SQLException e) {
ShowSQLException(out, e);
}
}
static public void ShowWarnings(PrintStream out, Connection theConnection) {
try {
// GET CONNECTION WARNINGS
SQLWarning warning = null;
if (theConnection != null) {
ShowWarnings(out, theConnection.getWarnings());
}
if (theConnection != null) {
theConnection.clearWarnings();
}
} catch (SQLException e) {
ShowSQLException(out, e);
}
}
/**
* Positive tests for forward only cursors.
*
* This method tests forward only cursors.
*
*
* @exception SQLException
* Thrown if some unexpected error happens
*/
public void testForwardOnlyPositive() throws SQLException {
Connection conn = getConnection();
ResultSet rs;
Statement s_f_r = createStatement(ResultSet.TYPE_FORWARD_ONLY,
ResultSet.CONCUR_READ_ONLY);
// We should have gotten no warnings and a read only forward only cursor
//JDBC.assertNoWarnings(conn.getWarnings());
conn.clearWarnings();
// Verify that setMaxRows(4) succeeds
s_f_r.setMaxRows(5);
assertEquals(5, s_f_r.getMaxRows());
rs = s_f_r.executeQuery("values 1, 2, 3, 4, 5, 6");
// Iterate straight thru RS, expect only 5 rows.
JDBC.assertDrainResults(rs, 5);
s_f_r.close();
}
/**
* Sakai modification: don't set autocommit, don't rollback if so configured!
*/
public void passivateObject(Object obj) throws Exception
{
if (obj instanceof Connection)
{
Connection conn = (Connection) obj;
if (m_rollbackOnReturn)
{
if (!conn.getAutoCommit() && !conn.isReadOnly())
{
Exception e = new RuntimeException("Automatic Transaction Rollback");
log.error("Transaction RolledBack!", e);
conn.rollback();
}
}
conn.clearWarnings();
// conn.setAutoCommit(true);
}
if (obj instanceof DelegatingConnection)
{
((DelegatingConnection) obj).passivate();
}
}
/**
* Calling any Connection method (except isClosed() and close()) on a proxy
* that was closed by closing the handler should throw an SQLException
* mentioning the connection was forcibly closed; the owner should not be
* notified of the exception.
*
* TODO: Consider testing for all Connection methods
*
* @throws SQLException
*/
@Test
public void testClosedHandler_throwsException() throws SQLException {
final FirebirdConnection physicalConnection = context.mock(FirebirdConnection.class);
final FBPooledConnection pooled = context.mock(FBPooledConnection.class);
final PooledConnectionHandler handler = new PooledConnectionHandler(physicalConnection,
pooled);
context.checking(new Expectations() {
{
connectionHandleReleaseExpectations(this, physicalConnection);
allowing(pooled).releaseConnectionHandler(handler);
allowing(pooled).fireConnectionClosed();
never(pooled).fireConnectionError(with(any(SQLException.class)));
}
});
Connection proxy = handler.getProxy();
handler.close();
expectedException.expect(SQLException.class);
expectedException.expectMessage(PooledConnectionHandler.FORCIBLY_CLOSED_MESSAGE);
proxy.clearWarnings();
}
/**
* Helper method available to all commands to dump any warnings
* associated with a connection. The set of warnings is cleared
* after display.
*
* @param session The session to use for writing
* @param conn The connection that may, or may not, contain warnings.
*/
static public void printWarnings(Session session, Connection conn) {
try {
SQLWarning w = conn.getWarnings();
if (w != null) {
printWarnings(session, w);
conn.clearWarnings();
}
}
catch (SQLException e) {
/* IGNORED */
}
}
public void passivateObject(Connection conn) throws Exception
{
if (!conn.getAutoCommit() && !conn.isReadOnly())
{
conn.rollback();
}
conn.clearWarnings();
if (!conn.getAutoCommit())
{
conn.setAutoCommit(true);
}
}
/**
* Calling a Connection method on an open proxy should notify the owner of
* the occurrence of an exception.
*
* @throws SQLException
*/
@Test
public void testException_Notify() throws SQLException {
final FirebirdConnection physicalConnection = context.mock(FirebirdConnection.class);
final FBPooledConnection pooled = context.mock(FBPooledConnection.class);
final PooledConnectionHandler handler = new PooledConnectionHandler(physicalConnection,
pooled);
final Sequence exceptionSequence = context.sequence("exceptionSequence");
context.checking(new Expectations() {
{
SQLException sqle = new FBSQLException("Mock Exception");
oneOf(physicalConnection).clearWarnings();
will(throwException(sqle));
inSequence(exceptionSequence);
oneOf(pooled).fireConnectionError(sqle);
inSequence(exceptionSequence);
}
});
Connection proxy = handler.getProxy();
expectedException.expect(SQLException.class);
proxy.clearWarnings();
}
/**
* close or reset the connection for reuse
* @param con
*/
public void close( Connection con ) {
if ( con != null ) {
try {
if ( dsDetails.perRequestConnections || dsDetails.isUnlimitedPool() ) { // reset for reuse within this request
con.clearWarnings();
con.setAutoCommit( true );
} else {
con.close(); // put back into the connection pool
}
} catch ( SQLException ignore ) {}
}
}
@Test
public void testNewInstance() throws SQLException {
Connection target = Mockito.mock(Connection.class);
ConnectionPoolCallback callback = Mockito.mock(ConnectionPoolCallback.class);
Connection proxy = newConnectionProxyFactory().newInstance(target, callback);
verify(callback, times(1)).acquireConnection();
verify(callback, never()).releaseConnection(anyLong());
proxy.clearWarnings();
proxy.close();
verify(target, times(1)).clearWarnings();
verify(callback, times(1)).releaseConnection(anyLong());
verifyNoMoreInteractions(callback);
}
/**
* CallableStatement tests.
*
* @exception SQLException
* Thrown if some unexpected error happens
*/
public void testCallableStatements() throws SQLException {
Connection conn = getConnection();
SQLWarning warning;
CallableStatement cs_s_r = null; // sensitive, read only
CallableStatement cs_s_u = null; // sensitive, updatable
CallableStatement cs_i_r = null; // insensitive, read only
CallableStatement cs_f_r = null; // forward only, read only
cs_s_r = prepareCall("values cast (? as Integer)",
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
// We should have gotten 1 warnings
warning = conn.getWarnings();
assertNotNull(warning);
if (!isDerbyNetClient)
assertEquals("01J02", warning.getSQLState());
else
assertEquals("01J10", warning.getSQLState());
JDBC.assertNoWarnings(warning.getNextWarning());
conn.clearWarnings();
cs_s_r.close();
cs_s_u = prepareCall("values cast (? as Integer)",
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
// We should have gotten 1 warning
warning = conn.getWarnings();
assertNotNull(warning);
if (!isDerbyNetClient)
assertEquals("01J02", warning.getSQLState());
else
assertEquals("01J10", warning.getSQLState());
JDBC.assertNoWarnings(warning.getNextWarning());
conn.clearWarnings();
cs_s_u.close();
cs_i_r = prepareCall("values cast (? as Integer)",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
// We should have gotten 0 warnings
JDBC.assertNoWarnings(conn.getWarnings());
conn.clearWarnings();
cs_i_r.close();
cs_f_r = prepareCall("values cast (? as Integer)",
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
// We should have gotten 0 warnings
JDBC.assertNoWarnings(conn.getWarnings());
conn.clearWarnings();
cs_f_r.close();
}
/**
* Scroll sensitive cursor tests
*
* This method tests scroll sensitive cursors. (Not implemented, so we
* should get back scroll insensitive curors with read only concurrency.)
*
* @exception SQLException
* Thrown if some unexpected error happens
*/
public void testScrollSensitive() throws SQLException {
Connection conn = getConnection();
ResultSet rs;
SQLWarning warning;
Statement s_s_r = null; // sensitive, read only
Statement s_s_u = null; // sensitive, updatable
s_s_r = createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// We should have gotten a warning and a scroll insensitive cursor
warning = conn.getWarnings();
assertNotNull(warning);
conn.clearWarnings();
// Verify that result set from statement is
// scroll insensitive and read only
rs = s_s_r.executeQuery("select * from t");
assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
rs.close();
// Close the statement
s_s_r.close();
//GemFireXD does not allow scrollable updatable cursors
/* s_s_u = createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// We should have gotten 1 warning and a updatable scroll
// insensitive cursor.
warning = conn.getWarnings();
assertNotNull(warning);
conn.clearWarnings();
// Verify that result set from statement is
// scroll insensitive and read only
rs = s_s_u.executeQuery("select * from t");
assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
rs.close();
*/
}
/**
* Scroll sensitive cursor tests
*
* This method tests scroll sensitive cursors. (Not implemented, so we
* should get back scroll insensitive curors with read only concurrency.)
*
* @exception SQLException
* Thrown if some unexpected error happens
*/
public void testScrollSensitive() throws SQLException {
Connection conn = getConnection();
ResultSet rs;
SQLWarning warning;
Statement s_s_r = null; // sensitive, read only
Statement s_s_u = null; // sensitive, updatable
s_s_r = createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// We should have gotten a warning and a scroll insensitive cursor
warning = conn.getWarnings();
assertNotNull(warning);
conn.clearWarnings();
// Verify that result set from statement is
// scroll insensitive and read only
rs = s_s_r.executeQuery("select * from t");
assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
rs.close();
// Close the statement
s_s_r.close();
//GemFireXD does not allow scrollable updatable cursors
/* s_s_u = createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// We should have gotten 1 warning and a updatable scroll
// insensitive cursor.
warning = conn.getWarnings();
assertNotNull(warning);
conn.clearWarnings();
// Verify that result set from statement is
// scroll insensitive and read only
rs = s_s_u.executeQuery("select * from t");
assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
rs.close();
*/
}
/**
* CallableStatement tests.
*
* @exception SQLException
* Thrown if some unexpected error happens
*/
public void testCallableStatements() throws SQLException {
Connection conn = getConnection();
SQLWarning warning;
CallableStatement cs_s_r = null; // sensitive, read only
CallableStatement cs_s_u = null; // sensitive, updatable
CallableStatement cs_i_r = null; // insensitive, read only
CallableStatement cs_f_r = null; // forward only, read only
cs_s_r = prepareCall("values cast (? as Integer)",
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
//GemFireXD throws different warnings, not equal to Derby
/*
// We should have gotten 1 warnings
warning = conn.getWarnings();
assertNotNull(warning);
if (!isDerbyNetClient)
assertEquals("01J02", warning.getSQLState());
else
assertEquals("01J10", warning.getSQLState());
//JDBC.assertNoWarnings(warning.getNextWarning());
conn.clearWarnings();
cs_s_r.close();
cs_s_u = prepareCall("values cast (? as Integer)",
ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
// We should have gotten 1 warning
warning = conn.getWarnings();
assertNotNull(warning);
if (!isDerbyNetClient)
assertEquals("01J02", warning.getSQLState());
else
assertEquals("01J10", warning.getSQLState());
//JDBC.assertNoWarnings(warning.getNextWarning());
conn.clearWarnings();
cs_s_u.close();
*/
cs_i_r = prepareCall("values cast (? as Integer)",
ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
// We should have gotten 0 warnings
//JDBC.assertNoWarnings(conn.getWarnings());
conn.clearWarnings();
cs_i_r.close();
cs_f_r = prepareCall("values cast (? as Integer)",
ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
// We should have gotten 0 warnings
//JDBC.assertNoWarnings(conn.getWarnings());
conn.clearWarnings();
cs_f_r.close();
}
/**
* Scroll sensitive cursor tests
*
* This method tests scroll sensitive cursors. (Not implemented, so we
* should get back scroll insensitive curors with read only concurrency.)
*
* @exception SQLException
* Thrown if some unexpected error happens
*/
public void testScrollSensitive() throws SQLException {
Connection conn = getConnection();
ResultSet rs;
SQLWarning warning;
Statement s_s_r = null; // sensitive, read only
Statement s_s_u = null; // sensitive, updatable
s_s_r = createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_READ_ONLY);
// We should have gotten a warning and a scroll insensitive cursor
warning = conn.getWarnings();
assertNotNull(warning);
conn.clearWarnings();
// Verify that result set from statement is
// scroll insensitive and read only
rs = s_s_r.executeQuery("select * from t");
assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
assertEquals(ResultSet.CONCUR_READ_ONLY, rs.getConcurrency());
rs.close();
// Close the statement
s_s_r.close();
s_s_u = createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,
ResultSet.CONCUR_UPDATABLE);
// We should have gotten 1 warning and a updatable scroll
// insensitive cursor.
warning = conn.getWarnings();
assertNotNull(warning);
conn.clearWarnings();
// Verify that result set from statement is
// scroll insensitive and read only
rs = s_s_u.executeQuery("select * from t");
assertEquals(ResultSet.TYPE_SCROLL_INSENSITIVE, rs.getType());
assertEquals(ResultSet.CONCUR_UPDATABLE, rs.getConcurrency());
rs.close();
}