18

I have two options to configure my application database connection - one is using JDBC, another one is using JNDI. What will be the best option in terms of how fast those connection types work with the database.

I understand those are two different types of database connections using different principles (JDBC is a direct db connection, JNDI is a database connection pool configuration on application server side). But are there other general JDBC/JNDI pros and cons which might be more important than operating speed? If yes, what are they?

altern
  • 5,829
  • 5
  • 44
  • 72
  • By JNDI you mean defining a Datasource on the server? – Tomer Jul 16 '12 at 08:05
  • @ftom2: yes, that's what I meant - configuring JNDI datasource on application server – altern Jul 16 '12 at 08:06
  • There is no any relationship in terms of performance between JNDI and JDBC, because JNDI returns some properties that helps application developer or application server to get database connection using JDBC; low level API for database connectivity. – rogue lad Feb 03 '14 at 07:03

8 Answers8

32

A database connection always uses JDBC. With JNDI you register a datasource in a directory service which can be looked up by its name. Thus JDBC and JNDI are completly different and not interchangeable.

stacker
  • 68,052
  • 28
  • 140
  • 210
22

I bet what you mean is choosing from

  1. creating datasource or jdbc connection manually in your application, or
  2. setup the datasource in the container, and application lookup the datasource through JNDI

If it is the case, always stick to 2 if possible.

The main reasons for the choice is never the performance differences. The reason for sticking to 2 is in most cases is, you need 2 to gain more advanced features from container, for example, distributed transaction.

Adrian Shum
  • 38,812
  • 10
  • 83
  • 131
3

This is what i have found about JNDI and JDBC.

JNDI: This is a technology which works like a telephone directory which is used to search the name on server and datasource remotely.

JNDI creates a connection pool. Connection pool is an environment on the server where JNDI and Database encapsulated to for Type4 connectivity.

JDBC: A Java API that enables Java programs to execute SQL statements. This allows Java programs to interact with any SQL-compliant database.

JDBC is similar to ODBC, but is designed specifically for Java programs, whereas ODBC is language-independent.

JDBC was developed by Sun Microsystems. JNDI is faster and efficient.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
eshwar
  • 1,763
  • 1
  • 18
  • 26
  • 1
    Again; I have notified you multiple times about the requirement to disclose when linking to your own website, but you have repeatedly failed to do so. – Andrew Barber Mar 05 '13 at 10:17
2

Not totally clear on the question.

JNDI isn't a type of database connection. You can use JNDI to look up a DataSource, which is a factory for connections. The DataSource is part of the JDBC API though, so JNDI works with JDBC as opposed to being alternatives here.

Are you talking about using JDBC against a database for directory information, vs. using JNDI against an LDAP repo?

2

The real speed benefit comes from being able to reuse database connections.

Hence, you need to use an approach which provides database connection pooling, and then use the appropriate technology to get to the pool. Depending on implementation this can be either JDBC (if the driver supports it itself) or JNDI or something completely different.

If your application runs inside a web container, it is common to use JNDI to allow the pool to be configured and managed in the web container instead of inside your application.

Thorbjørn Ravn Andersen
  • 73,784
  • 33
  • 194
  • 347
1

As mentioned in previous answers, using Datasource is the same as using JDBC in terms of technology.

Nevertheless, using a Datasource is usually the preffered way because that way you have the server managing your DB connection pools.

Tomer
  • 17,787
  • 15
  • 78
  • 137
1

Whether connection pooling is used does not affect application code. It does not require any code changes to the application because the application performs a lookup on a JNDI name of a previously registered data source. If the data source specifies a connection pooling implementation during JNDI registration (as described in section Creating a Data Source Using the DataDirect Connection Pool Manager), the client application benefits from faster connections through connection pooling.

Lion
  • 18,729
  • 22
  • 80
  • 110
1

The question is meaningless. Faster at what? There is nothing to compare. JDBC is a general-purpose interface to relational databases. JNDI is a general-purpose interface to naming systems. The strong probability is that the efficiency of either depends 99% on the target system being communicated with. In any case relational databases and naming systems fulfil completely different needs that are largely non-comparable. Usually JNDI is used to obtain a connection, then JDBC is used to operate with that connection.

user207421
  • 305,947
  • 44
  • 307
  • 483