I'm a long time .NET guy, very capable in my own element. However, I'm looking into some Java, and very specifically, at this moment, I'm trying to wrap my head around JDBC.
In ADO.NET, when you establish context to a particular Data Source, you use a connection right? I can see the similarity with JDBC.
What I wanted to know, is, do I treat that connection the same way I would in ADO.NET? In ADO.NET, when you close a connection, it releases it into a connection pool, so it can be consumed again. It keeps code really straight forward.
a typical ADO.NET insert example:
using (SqlConnection connection = Factory.CreateConnection()) {
connection.Open();
using (SqlCommand command = new SqlCommand("INSERT INTO people (name, age) VALUES (@name, @age)", connection)) {
command.Parameters.AddWithValue("@name", "Sammy SamSam");
command.Parameters.AddWithValue("@age", "23");
int records_affected = command.ExecuteNonQuery();
}
}
So, this is actually pretty optimized in .NET, we create and dispose of the connection every time we hit this method, but the actual connection is pooled and retained behind the scenes, keeping overhead to a minimum.
Do I do the same thing in Java? Or should I persist a connection somewhere, and then always reference it?
Thanks


Sign In
Create Account


Back to top









