Ideally, how many connections do I need to open?

I recently attended an interview in java, the interviewer asked a question, as shown below:

I have a request that processes modules A, B, C and the response goes back to A, in the AI ​​module you need to talk to the database, and again in the CI module you need to talk to the database, so in this situation how many connections will you open and where are you close these connections?

My answer: I said that the connection will open in the AI ​​module, and I will close it then and there, and then go to module B, then module C, to module C I will open another connection again, and I will close it again. then he asked me another question again: I want to open one connection to process the request, how can I do this?

+3
source share
5 answers

Sharing connections in modules.

EDIT You can share a connection by passing it to various modules:

class ModuleA {
    void process(){
        Connection c = getConnection();
        doA();
        new ModuleB( c ).doBs();
        new ModuleC( c ).doCs();
        doSomeMoreA();
        c.close();
    }
}

You can also have a connection in the outer class and look for it if necessary (possibly using sessionid)

I'm not sure if this counts as "Pool Connection"

It doesn't matter here.

class Db {
    private static Map<Integer, Connection> map;

    public static Connection getConnection( int sessionId ){

        if( !map.containsKey( sessionId ) ) {
            map.put( sessionId, createConnection());
        }
        return map.get( sessionId );
    }
}
class Main {
    void processs() {
        int sessionId = createSesionId();
        ModuleA a = new ModuleA( sessionId );
        a.doAsStuff();
        ModuleB b = new ModuleB( sessionId );
        b.doBsStuff();
        ModuleC c = new ModuleC( sessionId );
        b.doCsStuff();
        a.closeTransaction();

    }
}
class ModuleA{

    public doAsStuff() {
        Connection c = Db.getConnection(sessionId);
        doSomethingWith( c );
    }
    public closeTransaction() {
        Connection c = Db.getConnection(sessionId);
        c.close();
    }
}
class ModuleB{

    public doBsStuff() {
        Connection c = Db.getConnection(sessionId);
        doSomethingWith( c );
    }
}
class ModuleC{

    public doCsStuff() {
        Connection c = Db.getConnection(sessionId);
        doSomethingWith( c );
    }
}
+4
source

I would have guessed that the answer he was looking for was to use ThreadLocaland end it in the filter at the end of the query, but it is difficult to solve the “best” solution without asking more questions about the architecture, the mentioned module area is wrapped in (and even in the context of the module word value .)

+3
source

, ORM, Hibernate, ( Hibernate ), , .

+1
+1

" , ?"

, ( , ), , .

", : , ?"

Make a filter (request-interceptor) that opens the connection before processing the request and ends it later. Maintain a connection in a static instance of ThreadLocal so that it can be shared with the current thread. Do not use a simple static connection, otherwise it will be used for all your threads and display unwanted results.

0
source

Source: https://habr.com/ru/post/1746059/


All Articles