/
PooledConnectionTest.java
120 lines (102 loc) · 3.8 KB
/
PooledConnectionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package org.mariadb.jdbc;
import org.junit.Test;
import javax.sql.*;
import java.sql.*;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;
class MyEventListener implements ConnectionEventListener,StatementEventListener
{
public SQLException sqlException;
public boolean closed;
public boolean connectionErrorOccured;
public boolean statementClosed;
public boolean statementErrorOccured;
public void connectionClosed(ConnectionEvent event) {
sqlException = event.getSQLException();
closed = true;
}
public void connectionErrorOccurred(ConnectionEvent event) {
sqlException = event.getSQLException();
connectionErrorOccured = true;
}
public MyEventListener() {
sqlException = null;
closed = false;
connectionErrorOccured = false;
}
public void statementClosed(StatementEvent event) {
statementClosed = true;
}
public void statementErrorOccurred(StatementEvent event) {
sqlException = event.getSQLException();
statementErrorOccured = true;
}
}
public class PooledConnectionTest extends BaseTest {
@Test
public void testPooledConnectionClosed() throws Exception {
ConnectionPoolDataSource ds = new MySQLDataSource(hostname, port, database);
PooledConnection pc = ds.getPooledConnection(username, password);
Connection c= pc.getConnection();
MyEventListener listener = new MyEventListener();
pc.addConnectionEventListener(listener);
c.close();
assertTrue(listener.closed);
/* Verify physical connection is still ok */
c.createStatement().execute("select 1");
/* close physical connection */
pc.close();
/* Now verify physical connection is gone */
try {
c.createStatement().execute("select 1");
assertFalse("should never get there", true);
} catch(Exception e) {
}
}
@Test
public void testPooledConnectionException() throws Exception {
ConnectionPoolDataSource ds = new MySQLDataSource(hostname, port, database);
PooledConnection pc = ds.getPooledConnection(username, password);
MyEventListener listener = new MyEventListener();
pc.addConnectionEventListener(listener);
MySQLConnection c = (MySQLConnection)pc.getConnection();
/* Ask server to abort the connection */
try {
c.createStatement().execute("KILL CONNECTION_ID()");
}
catch (Exception e) {
/* exception is expected here, server sends query aborted */
}
/* Try to read after server side closed the connection */
try {
c.createStatement().execute("SELECT 1");
assertTrue("should never get there", false);
}
catch (Exception e) {
/* Check that listener was actually called*/
assertTrue(listener.sqlException instanceof SQLNonTransientConnectionException);
}
pc.close();
//assertTrue(listener.closed);
}
@Test
public void testPooledConnectionStatementError() throws Exception
{
ConnectionPoolDataSource ds = new MySQLDataSource(hostname, port, database);
PooledConnection pc = ds.getPooledConnection(username, password);
MyEventListener listener = new MyEventListener();
pc.addStatementEventListener(listener);
MySQLConnection c = (MySQLConnection)pc.getConnection();
PreparedStatement ps = c.prepareStatement("zzzz");
try {
ps.execute();
assertTrue("should never get there", false);
}
catch(Exception e) {
assertTrue(listener.statementErrorOccured && listener.sqlException instanceof SQLSyntaxErrorException);
}
ps.close();
assertTrue(listener.statementClosed);
pc.close();
}
}