Citigroup Interview Question for Financial Software Developers


Country: United States




Comment hidden because of low score. Click to expand.
1
of 1 vote

Below is the connection pooling at the most basic level(for database connections ). One can easily put pool size and time out in it. To give the user the flexibility to use close method on connection we will have to provide a wrapper for connection and implement several methods.

package connection_pooling;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.LinkedList;
import java.util.List;

public class MyConnectionPool {

	static List<Connection> pool;
	
	static{
		pool=new LinkedList<Connection>();
		for (int i = 0; i < 5; i++) {
		
			Connection connection = null;
			
			try{
			Class.forName("org.postgresql.Driver");
			connection = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/mydb", "userName","Password");
			pool.add(connection);
			}catch(Exception e){
				System.out.println(e);
			}
			
		}
	}
	
	
	public static Connection getConnection(){

		if(pool.size()>0){
			return pool.remove(pool.size()-1);
		}else{
			throw new Error("no conn available");
		}
	}
	
	public static void returnConnection(Connection conn){
		pool.add(conn);
	}
	
	public static void main(String[] args) {
		Connection conn = MyConnectionPool.getConnection();
		//do something
		MyConnectionPool.returnConnection(conn);
	}
	
}

- praveenkcs28 April 20, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

One thing to remember when writing connection-pools: never pass the raw connection to your caller. Put it in a wrapper object that has no-ops for calls like close() //connection, so that the raw connection is protected, and pass it via interface. When a connectionWrapper is returned by user, put the raw connection back in your pool, but also null out the reference from the wrapper to the raw-connection. Also good idea would be to rollback any uncommited transactions on the raw connection before putting it in pool

- Amit January 24, 2017 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More