/
ChangeCatalogConnectionHandler.java
50 lines (40 loc) · 1.36 KB
/
ChangeCatalogConnectionHandler.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
package org.jumpmind.db.sql;
import java.sql.Connection;
import java.sql.SQLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class ChangeCatalogConnectionHandler implements IConnectionHandler {
protected final Logger log = LoggerFactory.getLogger(getClass());
private String previousCatalog;
private String changeCatalog;
public ChangeCatalogConnectionHandler(String newCatalog) {
changeCatalog = newCatalog;
}
@Override
public void before(Connection connection) {
if (changeCatalog != null) {
try {
previousCatalog = connection.getCatalog();
connection.setCatalog(changeCatalog);
} catch (SQLException e) {
log.warn("Unable to switch to catalog '{}': ", changeCatalog, e.getMessage());
if (changeCatalog != null) {
try {
connection.setCatalog(previousCatalog);
} catch (SQLException ex) {
}
}
throw new SqlException(e);
}
}
}
@Override
public void after(Connection connection) {
try {
if (previousCatalog != null) {
connection.setCatalog(previousCatalog);
}
} catch (SQLException ex) {
}
}
}