Markus Pitha
2008-03-06 23:09:03 UTC
Hallo,
da ich in mehreren Klassen Datenbankverbindungen brauche, die auch noch
effizient sein sollen, habe ich frühere Tipps von hier versucht zu
befolgen, und einen Pool mit einem Singleton versucht zu verwenden.
So sieht mein funktionierendes Beispiel aus:
----------Testsingleton.class---------
import java.sql.Connection;
import java.sql.SQLException;
import org.postgresql.jdbc2.optional.PoolingDataSource;
public final class TestSingleton {
private static TestSingleton instance;
private static PoolingDataSource pds;
private TestSingleton() throws ClassNotFoundException, SQLException {
pds = new PoolingDataSource();
pds.setServerName("localhost");
pds.setPassword("pwd123456");
pds.setUser("postgres");
pds.setDatabaseName("testdb");
pds.setMaxConnections(20);
}
public synchronized static TestSingleton getInstance() throws
ClassNotFoundException, SQLException {
if (instance == null) {
instance = new TestSingleton();
}
return instance;
}
public synchronized static java.sql.Connection
getPoolConnection() throws SQLException {
return pds.getConnection();
}
public synchronized static void closePoolConnection(Connection c) {
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (c != null)
try {
c.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
----------------Testklasse.class---------------
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Testklasse {
public void testeAbfrage() throws SQLException {
Connection con = null;
PreparedStatement pstmt;
con = TestSingleton.getPoolConnection();
pstmt = con.prepareStatement("SELECT vorname,nachname,datum from
testtabelle " +
"WHERE vorname = ?");
pstmt.setString(1, "Tester");
ResultSet rs = pstmt.executeQuery();
while (rs.next())
System.out.println(rs.getString(1) + " " + rs.getString(2) +
" " + rs.getDate(3));
TestSingleton.closePoolConnection(con);
}
}
---------------Main.class-----------------------
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
TestSingleton.getInstance();
Testklasse tk = new Testklasse();
tk.testeAbfrage();
}
}
Das Beispiel funktioniert, aber ist es auch so richtig implementiert?
Mit Connection Pools und Singletons habe ich nämlich bisher keine
Erfahrung.
Ich bedanke mich schon mal im vorhinein für etwaige Antworten.
Markus
da ich in mehreren Klassen Datenbankverbindungen brauche, die auch noch
effizient sein sollen, habe ich frühere Tipps von hier versucht zu
befolgen, und einen Pool mit einem Singleton versucht zu verwenden.
So sieht mein funktionierendes Beispiel aus:
----------Testsingleton.class---------
import java.sql.Connection;
import java.sql.SQLException;
import org.postgresql.jdbc2.optional.PoolingDataSource;
public final class TestSingleton {
private static TestSingleton instance;
private static PoolingDataSource pds;
private TestSingleton() throws ClassNotFoundException, SQLException {
pds = new PoolingDataSource();
pds.setServerName("localhost");
pds.setPassword("pwd123456");
pds.setUser("postgres");
pds.setDatabaseName("testdb");
pds.setMaxConnections(20);
}
public synchronized static TestSingleton getInstance() throws
ClassNotFoundException, SQLException {
if (instance == null) {
instance = new TestSingleton();
}
return instance;
}
public synchronized static java.sql.Connection
getPoolConnection() throws SQLException {
return pds.getConnection();
}
public synchronized static void closePoolConnection(Connection c) {
try {
c.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (c != null)
try {
c.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
----------------Testklasse.class---------------
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class Testklasse {
public void testeAbfrage() throws SQLException {
Connection con = null;
PreparedStatement pstmt;
con = TestSingleton.getPoolConnection();
pstmt = con.prepareStatement("SELECT vorname,nachname,datum from
testtabelle " +
"WHERE vorname = ?");
pstmt.setString(1, "Tester");
ResultSet rs = pstmt.executeQuery();
while (rs.next())
System.out.println(rs.getString(1) + " " + rs.getString(2) +
" " + rs.getDate(3));
TestSingleton.closePoolConnection(con);
}
}
---------------Main.class-----------------------
import java.sql.SQLException;
public class Main {
public static void main(String[] args) throws ClassNotFoundException,
SQLException {
TestSingleton.getInstance();
Testklasse tk = new Testklasse();
tk.testeAbfrage();
}
}
Das Beispiel funktioniert, aber ist es auch so richtig implementiert?
Mit Connection Pools und Singletons habe ich nämlich bisher keine
Erfahrung.
Ich bedanke mich schon mal im vorhinein für etwaige Antworten.
Markus