I want to know which is safe & better way to use Connection variable of following.
First : Connection as class member variable of class
class MyClass
{
Connection conn;
public MyClass(Connection conn) {
this.conn = conn;
}
public void myMethod(){
//Do some DB operations using conn
}
}
Second : Initialize connection in method
class MyClass
{
public MyClass() {
}
public void myMethod(){
Connection conn= initializeFunction(); //Initialize Connection
//Do some DB operations using conn
}
}
Third : Send connection as argument to function
class MyClass
{
public MyClass() {
}
public void myMethod(Connection conn){
//Do some DB operations using conn
}
}
NOTE : Question is not programming language specific, hence I have added tags other than Java as well.
All three approaches provide different functionality:
The choice depends on:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With