Jump to content

JDBC vs. ADO.NET

- - - - -

  • Please log in to reply
2 replies to this topic

#1
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
Hey Guys,
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

#2
bobdark

bobdark

    Programmer

  • Members
  • PipPipPipPip
  • 164 posts
In JDBC connection pool isn't used by default.

#3
sam_coder

sam_coder

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 372 posts
Thanks for the response.

is it configurable in the connection url? or would jdbc expect that I should create one connection instance, and use it all throughout the application?

ADO.NET has a limitation, where a single connection can be consumed only once at a time, and cannot be used again until whatever is consuming it has been disposed.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users