Singleton variant
Jeg har en database pool jeg ønsker skal være en singleton klasse, jeg har lavet denne klasse og jeg ved godt at den ikke overholder mønstreret 100 % som beskrevet i bøgerne, men virker getInstance() ikke efter singleton mønsteret, altså returnerer kun et og kun det samme objekt hver gang. Grunden til jeg spørger er netop p.g.a af varitionen.virker det efter mønsteret ?
klassen kommer her.
package test;
import org.firebirdsql.jdbc.FBWrappingDataSource;
import java.sql.SQLException;
import javax.resource.ResourceException;
/**
* DOCUMENT ME!
*
* @author $author$
* @version $Revision$
*/
public class FBWrappingDataSourceSingleton {
private static final String DATABASEURL =
"localhost/3050:C:\\Programmer\\IBOConsole\\TEST";
private static final int IDLE_MINS = 30;
private static final int MIN_CONNECTIONS = 5;
private static final int MAX_CONNECTIONS = 40;
private static final int MAX_SIM_CONNECTIONS = 20;
private static FBWrappingDataSource fbwds;
//singleton object
private static FBWrappingDataSourceSingleton singleton;
/**
* Constructor for FBSingletonConPool.
*/
private FBWrappingDataSourceSingleton() {
try {
fbwds = new FBWrappingDataSource();
} catch (ResourceException e) {
System.err.println(e.getMessage());
e.printStackTrace();
}
fbwds.setDatabase(DATABASEURL);
// an old format version of the same url
// fbwds.setDatabase("localhost/3050:/dir1/subdir/myDatabase.gdb");
fbwds.setUserName("sysdba");
fbwds.setPassword("masterkey");
fbwds.setIdleTimeoutMinutes(IDLE_MINS);
fbwds.setPooling(true);
// this turns on pooling for this data source. Max and min must be set.
fbwds.setMinSize(MIN_CONNECTIONS);
// this sets the minimum number of connections to keep in the pool
fbwds.setMaxSize(MAX_CONNECTIONS);
// this sets the maximum number of connections that can be open at one time.
try {
fbwds.setLoginTimeout(MAX_SIM_CONNECTIONS);
} catch (SQLException e) {
System.out.println("Could not set Login Timeout in SQLDriver\n");
}
}
/**
*
*
* @return DOCUMENT ME!
*/
public static synchronized FBWrappingDataSource getInstance() {
if (singleton == null) {
singleton = new FBWrappingDataSourceSingleton();
}
return fbwds;
}
}