Er treedt een concurrency probleem op a.d.h.v. de 2 onderstaande klassen. Vermoedelijk moet de methode getConnection aangepast worden zodat er elke keer een nieuwe connectie wordt aangemaakt in plaats van te werken met de statische variabele. Wie kan hier mee helpen of heeft een beter voorstel?
Java:
1
2
3
4
5
6
7
8
| public class Connector { private static final String DB_NAME = "jdbc:odbc:test"; private static Connection conn = null; public static Connection getConnection() { return getConnection(DB_NAME); } |
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
| public static Connection getConnection(String dbUrl) { try{ if(conn == null || conn.isClosed()){ synchronized (Connector.class) { if(conn == null || conn.isClosed()){ try{ // Load the JDBC-ODBC bridge driver used // for connecting to an ODBC data source... Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // Retrieve a connection to the database... conn = DriverManager.getConnection(dbUrl); }catch(ClassNotFoundException cnfe){ System.err.println("ClassNotFoundException "+cnfe.getMessage()); }catch(SQLException se){ System.err.println("SQLException "+se.getMessage()); }catch(NullPointerException npe){ System.err.println("NullPointerException "+npe.getMessage()); }catch(Exception e){ System.err.println("Exception "+e.getMessage()); } } } } } catch(SQLException se){ System.err.println("SQLException "+se.getMessage()); } return conn; } } |