What is the difference between jdbc and datasource
A connection pool is a cache of database connections maintained by the database. In connection pooling, once the connection created and it can be reused again and again. There is no need of new connection. New connection is needed only when all the available connections are in use. Connection pooling also cuts down the amount of time that a user need to wait to establish a connection to the database. JNDI is a actual interface between App server and data-source.
JNDI gives unique name for each and every server. ODBC drivers convert into the requirements of databases. Type 4 Driver: This driver directly converts the java statements to SQl Statements which require to use in database.
It wont convert to JDBC statement. Posted by kiran kumar setty on October 12, in Uncategorized. You are commenting using your WordPress. You are commenting using your Google account.
You are commenting using your Twitter account. You are commenting using your Facebook account. Notify me of new comments via email. Notify me of new posts via email. Improve this answer. Will the connection using datasource faster than the connection using drivermanager? Sign up or log in Sign up using Google.
Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Does ES6 make JavaScript frameworks obsolete? Podcast Do polyglots have an edge when it comes to mastering programming Featured on Meta.
Now live: A fully responsive profile. Related Hot Network Questions. A system administrator or another person working in that capacity can deploy a DataSource object so that the connections it produces are pooled connections. To do this, he or she first deploys a ConnectionPoolDataSource object and then deploys a DataSource object implemented to work with it. The properties of the ConnectionPoolDataSource object are set so that it represents the data source to which connections will be produced.
Generally only two properties must be set for the DataSource object: description and dataSourceName. The value given to the dataSourceName property is the logical name identifying the ConnectionPoolDataSource object previously deployed, which is the object containing the properties needed to make the connection. This connection will be to the data source specified in the ConnectionPoolDataSource object's properties. The following example describes how a system administrator for The Coffee Break would deploy a DataSource object implemented to provide pooled connections.
The system administrator would typically use a deployment tool, so the code fragments shown in this section are the code that a deployment tool would execute. The system administrator creates an instance of this class, sets its properties, and registers it with a JNDI naming service. The Coffee Break has bought its DataSource class, com. The class com. PooledDataSource implements connection pooling by using the underlying support provided by the ConnectionPoolDataSource class com.
The ConnectionPoolDataSource object must be deployed first. The following code creates an instance of com. ConnectionPoolDS and sets its properties:. The following code registers the com. Note that the logical name being associated with the cpds variable has the subcontext pool added under the subcontext jdbc , which is similar to adding a subdirectory to another subdirectory in a hierarchical file system.
The logical name of any instance of the class com. Next, the DataSource class that is implemented to interact with the cpds variable and other instances of the com. ConnectionPoolDS class is deployed. The following code creates an instance of this class and sets its properties. Note that only two properties are set for this instance of com.
The description property is set because it is always required. The other property that is set, dataSourceName , gives the logical JNDI name for cpds , which is an instance of the com.
ConnectionPoolDS class. In other words, cpds represents the ConnectionPoolDataSource object that will implement connection pooling for the DataSource object.
A connection pool is a cache of database connection objects. The objects represent physical database connections that can be used by an application to connect to a database. At run time, the application requests a connection from the pool. If the pool contains a connection that can satisfy the request, it returns the connection to the application. If no connections are found, a new connection is created and returned to the application. The application uses the connection to perform some work on the database and then returns the object back to the pool.
The connection is then available for the next connection request. Connection pools promote the reuse of connection objects and reduce the number of times that connection objects are created. Connection pools significantly improve performance for database-intensive applications because creating connection objects is costly both in terms of time and resources. The code for getting a pooled connection is just like the code for getting a nonpooled connection, as shown in the following two lines:.
You need to retrieve this DataSource object only once because you can use it to produce as many pooled connections as needed. Calling the method getConnection on the ds variable automatically produces a pooled connection because the DataSource object that the ds variable represents was configured to produce pooled connections. Connection pooling is generally transparent to the programmer.
There are only two things you need to do when you are using pooled connections:. Use a DataSource object rather than the DriverManager class to get a connection. In the following line of code, ds is a DataSource object implemented and deployed so that it will create pooled connections and username and password are variables that represent the credentials of the user that has access to the database:.
Use a finally statement to close a pooled connection. Otherwise, an application using a pooled connection is identical to an application using a regular connection. The only other thing an application programmer might notice when connection pooling is being done is that performance is better. The connection in this code sample participates in connection pooling because the following are true:.
Note that although this code is very similar to code you have seen before, it is different in the following ways:. It imports the javax. The purpose of this example is to show that you use a pooled connection the same way you use a nonpooled connection, so you need not worry about understanding the EJB API. It uses a DataSource object to get a connection instead of using the DriverManager facility.
Getting and using a pooled connection is similar to getting and using a regular connection. When someone acting as a system administrator has deployed a ConnectionPoolDataSource object and a DataSource object properly, an application uses that DataSource object to get a pooled connection.