0

I use singleton to increase response for database connection. But when there is many users that which send request in same time, instances of one connection make a bottle neck, So I use another approach in getInstace() method :

public class jjDatabaseWeb {

    private static final DatabaseType dbType = DatabaseType.MySql;
    private static String password;
    private ResultSet resultSet;
    private static String serverHost = "127.0.0.1"; //localhost";
    private static String port = "3306";
    private static String databaseName = "db_university";
    static String url2;

    private Connection connection;
    private static jjDatabaseWeb accessor;
    private static jjDatabaseWeb accessor2;
    private static jjDatabaseWeb accessor3;
    private static jjDatabaseWeb accessor4;
    private static jjDatabaseWeb accessor5;
    private static jjDatabaseWeb accessor6;
    private static jjDatabaseWeb accessor7;
    private static jjDatabaseWeb accessor8;
    private static jjDatabaseWeb accessor9;
    private static jjDatabaseWeb accessor10;
    private static int counter = 0;
    private static int counter2 = 0;

    private jjDatabaseWeb() throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/db_university?characterSetResults=UTF-8&characterEncoding=UTF-8&useUnicode=yes", "root", "m123456");        
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        System.out.println("database driver name is ok");
    }


    public static synchronized jjDatabaseWeb getInstance() throws SQLException, ClassNotFoundException {
        counter++;
        System.out.println("+++++=====>>>> number of connection user" + counter);
        if (accessor == null) {
            System.out.println("############################################");
            System.out.println("NEW DB CONNECTION 1 ");
            System.out.println("############################################");
            accessor = new jjDatabaseWeb();
        }
        if (counter > 7) {
            System.out.println("-------------------------------" + counter2);
            counter2++;
            if (counter2 % 10 == 1) {
                if (accessor2 == null) {
                    System.out.println("############################################");
                    System.out.println("NEW DB CONNECTION 2");
                    System.out.println("############################################");
                    accessor2 = new jjDatabaseWeb();
                }
                return accessor2;
            }
            if (counter2 % 10 == 2) {
                if (accessor3 == null) {
                    System.out.println("############################################");
                    System.out.println("NEW DB CONNECTION 3");
                    System.out.println("############################################");
                    accessor3 = new jjDatabaseWeb();
                }
                return accessor3;
            }
            if (counter2 % 10 == 3) {
                if (accessor4 == null) {
                    System.out.println("############################################");
                    System.out.println("NEW DB CONNECTION 4");
                    System.out.println("############################################");
                    accessor4 = new jjDatabaseWeb();
                }
                return accessor4;
            }
            if (counter2 % 10 == 4) {
                if (accessor5 == null) {
                    System.out.println("############################################");
                    System.out.println("NEW DB CONNECTION 5");
                    System.out.println("############################################");
                    accessor5 = new jjDatabaseWeb();
                }
                return accessor5;
            }
            if (counter2 % 10 == 5) {
                if (accessor6 == null) {
                    System.out.println("############################################");
                    System.out.println("NEW DB CONNECTION 6");
                    System.out.println("############################################");
                    accessor6 = new jjDatabaseWeb();
                }
                return accessor6;
            }
            if (counter2 % 10 == 6) {
                if (accessor7 == null) {
                    System.out.println("############################################");
                    System.out.println("NEW DB CONNECTION 7");
                    System.out.println("############################################");
                    accessor7 = new jjDatabaseWeb();
                }
                return accessor7;
            }
            if (counter2 % 10 == 7) {
                if (accessor8 == null) {
                    System.out.println("############################################");
                    System.out.println("NEW DB CONNECTION 8");
                    System.out.println("############################################");
                    accessor8 = new jjDatabaseWeb();
                }
                return accessor8;
            }
            if (counter2 % 10 == 8) {
                if (accessor9 == null) {
                    System.out.println("############################################");
                    System.out.println("NEW DB CONNECTION 9");
                    System.out.println("############################################");
                    accessor9 = new jjDatabaseWeb();
                }
                return accessor9;
            }
            if (counter2 % 10 == 9) {
                if (accessor10 == null) {
                    System.out.println("############################################");
                    System.out.println("NEW DB CONNECTION 10");
                    System.out.println("############################################");
                    accessor10 = new jjDatabaseWeb();
                }
                return accessor10;
            }
        }
        counter2 = 0;
        return accessor;
    }
}

I never closed connection And use singleton, it has some benefit.

  • If there was created connection,new connection dose not makes, so fetching data is faster(making connection is time consuming )

  • In non busy time we have one connection is single tone mode and if there is max 7 request concurrent the program get instance of one connection.

  • In busy time when there is many requests waited for one connection, it make 9 other connection and share new requests between them.

I test it in wrong condition, and send 40 request in one second from 3 different browser to one method and it takes 2 minutes, but when i use only singleton it takes about 6 minutes.

But when i repeat this test some error occurred for some requests like "Heap space error" and Java invocation target exception.

note that in output i saw counter=28 (that means there was 28,27 in peak task waited for connection)

I have not problem for first time running of batch requests , but when i repeat tis test it has some error reason of heap space lake.

Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
MrSalesi
  • 377
  • 3
  • 17
  • 5
    Use a Connection Pool! – Elliott Frisch Oct 30 '15 at 11:05
  • 1
    Better to use connection pool like Apache DBCP. Look [here](http://www.journaldev.com/2509/jdbc-datasource-example-oracle-mysql-and-apache-dbcp-tutorial) for more information – Vivek Singh Oct 30 '15 at 11:07
  • 2
    X-Y-Problem. This question is probably not going to get an answer, because the solution to your problem is: do not reinvent the wheel (and fail trying). – Fildor Oct 30 '15 at 11:32
  • 2
    Your code looks like you are a beginner with Java: you use static all over the place (which makes unit testing very hard; so it seems you are not testing your code, which is a bad thing, too). Then, you seem to not have heard of arrays or Java collections; nobody should start giving variables names like a1, a2, a3; and then have such if cadences as you have there. Long story short: before you try to put up real world projects that are supposed to be used by other people: spent some time learning the **essential java basics**. Btw: what actions would decrease your counters? – GhostCat Oct 30 '15 at 11:40
  • Dear Jägermeister fast judge, I can use array,BUT "using Array" is not my problem. Other wise my server has limit in database connection (MAX 10). Others found out all problem and they told there idea. If you don't have solution , you are not force to say, Dear Jägermeister . – MrSalesi Oct 30 '15 at 15:55
  • are you using a java ee application server? – alacambra Nov 01 '15 at 18:01

1 Answers1

0

i think it is better to leave your application server handles the connections for you , via it's connection pool (you can set min and max connection pool from application server configuration).