Skip to content

Commit 16bdc2e

Browse files
author
Markus M. Geipel
committed
Merge pull request #59 from schaeferd/master
JndiSqlMap - closed Connection aufter query
2 parents 1d3f290 + 5057c43 commit 16bdc2e

File tree

1 file changed

+90
-84
lines changed

1 file changed

+90
-84
lines changed

src/main/java/org/culturegraph/mf/morph/maps/JndiSqlMap.java

Lines changed: 90 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,91 +13,97 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.culturegraph.mf.morph.maps;
17-
18-
import java.io.Closeable;
19-
import java.io.IOException;
20-
import java.sql.Connection;
21-
import java.sql.PreparedStatement;
22-
import java.sql.ResultSet;
23-
import java.sql.SQLException;
24-
25-
import javax.naming.InitialContext;
26-
import javax.naming.NamingException;
27-
import javax.sql.DataSource;
28-
29-
import org.culturegraph.mf.exceptions.MorphException;
30-
31-
32-
/**
33-
* @author Daniel
34-
*
35-
*/
36-
public final class JndiSqlMap extends AbstractReadOnlyMap<String, String> implements Closeable {
37-
38-
private boolean isUninitialized = true;
39-
private DataSource datasource;
40-
private String query;
16+
package org.culturegraph.mf.morph.maps;
17+
18+
import java.io.Closeable;
19+
import java.io.IOException;
20+
import java.sql.Connection;
21+
import java.sql.PreparedStatement;
22+
import java.sql.ResultSet;
23+
import java.sql.SQLException;
24+
import java.sql.Statement;
25+
import javax.naming.InitialContext;
26+
import javax.naming.NamingException;
27+
import javax.sql.DataSource;
28+
29+
import org.culturegraph.mf.exceptions.MorphException;
30+
import org.slf4j.Logger;
31+
import org.slf4j.LoggerFactory;
32+
33+
34+
/**
35+
* @author Daniel
36+
*
37+
*/
38+
public final class JndiSqlMap extends AbstractReadOnlyMap<String, String> implements Closeable {
39+
40+
private static final Logger LOG = LoggerFactory.getLogger(JndiSqlMap.class);
41+
private DataSource datasource;
42+
private String query;
4143
private Connection connection;
42-
43-
private PreparedStatement preparedStatement;
44-
45-
public void init() {
46-
47-
try {
48-
connection = datasource.getConnection();
49-
this.preparedStatement = connection.prepareStatement(query);
50-
} catch (SQLException e) {
51-
throw new MorphException(e);
52-
}
53-
isUninitialized = false;
54-
}
55-
56-
@Override
57-
public void close() throws IOException {
58-
try {
59-
if (connection != null) {
60-
connection.close();
61-
}
62-
} catch (SQLException e) {
63-
throw new MorphException(e);
64-
}
65-
}
66-
67-
protected DataSource getDatasource() {
68-
return datasource;
69-
}
70-
71-
public void setDatasource(final String name) {
72-
try {
73-
this.datasource = (DataSource) new InitialContext().lookup(name);
74-
} catch (NamingException e) {
75-
throw new MorphException(e);
76-
}
77-
}
78-
79-
public void setQuery(final String query) {
80-
this.query = query;
81-
}
82-
83-
@Override
84-
public String get(final Object key) {
85-
if (isUninitialized) {
86-
init();
87-
}
88-
String resultString = null;
44+
45+
46+
@Override
47+
public void close() throws IOException {
48+
try {
49+
if (connection != null) {
50+
connection.close();
51+
}
52+
} catch (SQLException e) {
53+
throw new MorphException(e);
54+
}
55+
}
56+
57+
protected DataSource getDatasource() {
58+
return datasource;
59+
}
60+
61+
public void setDatasource(final String name) {
62+
try {
63+
this.datasource = (DataSource) new InitialContext().lookup(name);
64+
} catch (NamingException e) {
65+
throw new MorphException(e);
66+
}
67+
}
68+
69+
public void setQuery(final String query) {
70+
this.query = query;
71+
}
72+
73+
@Override
74+
public String get(final Object key) {
75+
String resultString = null;
8976
final ResultSet resultSet;
77+
PreparedStatement stmt = null;
78+
Connection con = null;
9079
try {
91-
preparedStatement.setString(1, key.toString());
92-
resultSet = preparedStatement.executeQuery();
93-
if (resultSet.first()) {
94-
resultString = resultSet.getString(1);
80+
con = datasource.getConnection();
81+
stmt = con.prepareStatement(query);
82+
stmt.setString(1, key.toString());
83+
resultSet = stmt.executeQuery();
84+
if (resultSet.first()) {
85+
resultString = resultSet.getString(1);
86+
}
87+
resultSet.close();
88+
} catch (SQLException e) {
89+
throw new MorphException(e);
90+
} finally {
91+
if(stmt != null){
92+
try {
93+
stmt.close();
94+
} catch (SQLException e) {
95+
LOG.error("Can't close SQL-Statement.", e);
96+
}
97+
}
98+
if(con != null){
99+
try {
100+
con.close();
101+
} catch (SQLException e) {
102+
LOG.error("Can't close Connection.", e);
103+
}
95104
}
96-
resultSet.close();
97-
} catch (SQLException e) {
98-
throw new MorphException(e);
99-
}
100-
return resultString;
101-
}
102-
103-
}
105+
}
106+
return resultString;
107+
}
108+
109+
}

0 commit comments

Comments
 (0)