In this post I am going to tell you how to create a new Data Source into WebLogic Server, Step By Step. This post is a prerequisite for the coming posts regarding J2EE development.
Creating data source in WebLogic Server
WebLogic JDBC data sources provide database access and database connection management. Each data source contains a pool of database connections that are created when the data source is created at server startup.
Applications reserve a database connection from the data source by looking up the data source on the JNDI tree or in the local application context and then calling getConnection() method, as it will appear in the context of this post.
To configure database connectivity In WebLogic Server, you must define a data sources into your WebLogic domain.
When you finish using the connection in your code, you should call connection.close() as early as possible, to returns the database connection to the pool for other applications to use.
Data sources and their connection pools provide connection management processes that help keep your system running and working.
To create a data source in weblogic domain :
1. Open your domain’s admin console, it will be of this form if the domain is installed locally on your machine: http://localhost:7001/console.
2. Enter the password for your domain console, and hit login button.
3. The administration page will open, on left tab, on second box, expand Services node and click on Data Sources.
4. From the Data Sources table that appears click on new > Generic Data Source.
5. Fill in the details of your data source as shown in the figure below, and choose your desired database. In this post I am using Oracle database, HR schema. Click Next.
6. Choose the database driver, and click Next twice until you reach the database details form.
7. Fill the details of your database, change the following according to your settings and click Next.
8. The next page will display the details of your new data source, click on Test Configuration button; if your connection details are correct you will get Connection test succeeded message, as shown below. Click Next.
9. Now you reach the last stage, you need to select the target server that your new data source will deployed on, select Admin Server and click Finish.
10. This is the last step, when you click Finish as mentioned above, you will be returned to the table of available data sources at your domain, with all the information regarding your new data source; Name, Type, JNDI Name, and Target servers. As shown below.
Using Data Source in your code
In the following we will see how to use our created data source. In your code, you need to initialize a context to lookup your data source as shown in the following snippet code.
After you got the connection, you can use it to execute a database operations against your data source, which was configured to point to Oracle database; HR schema.
Closing the Connection
To prevent resources leaks in your program, you need to close the opened resources. Closing the resources is not your choice, it is the compiler choice to close it, you only mark these opened resources for garbage collection, but you cannot enforce it.
In java, you need to call method connection.close() to mark your data source for garbage collection, and to make it available for other requests to your program.
For now, do not worry about the above code, I am going to post a full example that shows how to use data source effectively, this post is a prerequisite for the coming posts regarding J2EE technology.
If you have any question or comment, please post it down.