สร้าง datasource สำหรับ tomcat
13 June 2023
เวลาที่เราจะพัฒนาระบบโดยใช้ java application server ไป connect ที่ database (ในตัวอย่างนี้จะใช้ database เป็น mysql) มีสิ่งที่จะต้องทำดังนี้
- รู้ว่า database ปลายทางเราอยู่ที่ไหน เช่น ชื่อเครื่อง หมายเลข IP หรือถ้าอยู่บน cloud เช่น aws ก็ต้องรู้ endpoint
- รู้ชื่อของ database ที่ต้องการจะ connect
- รู้ user/password ที่จะใช้ในการ connect ไปที่ database
เมื่อเรารู้ครบทั้ง 3 อย่างนี้แล้วก็ให้ไปที่ตัว tomcat server ของเรา
จากนั้นสิ่งที่จะต้องทำคือ
- ติดตั้ง jdbc driver
- สร้าง datasource สำหรับใช้ connect ไปที่ database ที่เราต้องการ
- ทดสอบว่า datasource ที่สร้างขึ้นใช้งานได้หรือไม่ (ก่อนส่งงานต่อให้ใครต้องทดสอบเองก่อนนะครับว่ามันใช้ได้)
การติดตั้ง jdbc driver ของ mysql ก็ไปทำการ download ได้จาก website ของ Oracle โดยต้องเลือกเป็น Platform Independent ด้วย จะได้ file เป็น .jar มา
เมื่อเรา download มาแล้วก็ให้ทำการแตก file ออกมา จะมี file ที่เราต้องการชื่อว่า “mysql-connector-j-8.0.33.jar” ให้ทำการ copy file นี้ไปไว้ที่ directory “lib” ของตัว tomcat server
จากนั้นให้ไปที่ directory <tomcat server>/conf แล้วทำการแก้ไข file “context.xml” โดยให้เพิ่ม datasource ที่เราต้องการที่ส่วน <Resource> โดยจะอยู่ในส่วนของ <Context>
จากรูปจะมีสิ่งที่สนใจคือ
name=”jdbc/myDataSource” คือชื่อของ datasource ที่จะนำไปให้ตัวโปรแกรมใช้อ้างอิง
url เป็น address ปลายทางของ database ที่เราต้องการ connect โดยถ้าเป็นเครื่องที่ไม่ได้อยู่บน cloud อาจจะอ้างอิงดังนี้
url=”jdbc:mysql://localhost/mydb” คือใส่ชื่อเครื่องตามด้วยชื่อของ database
หรือหากเป็น rds บน cloud (aws) จะอ้างอิงดังนี้
url=”jdbc:mysql://your-rds-instance-endpoint:your-rds-port” (เลข port ถ้าใช้ 3306 ตาม default ก็อาจจะไม่ต้องใส่ก็ได้)
username และ password เป็น user/password ที่ใช้ในการ connect database
หลังจากแก้ไข file “context.xml” เสร็จแล้วก็ให้ทำการบันทึก แล้วทำการ restart tomcat server เพื่อให้รู้จัก datasource ที่เราเพิ่มเข้าไป
จากนั้นก็ถึงขั้นตอนสุดท้าย คือการทดสอบว่า datasource ที่เราสร้างนั้นสามารถนำไปใช้งานได้หรือไม่ โดยให้เราสร้างโปรแกรมเป็น jsp ขึ้นมา 1 ตัวเพื่อใช้ทดสอบ โดยในตัวอย่างนี้จะสร้างโปรแกรมชื่อ “TestDataSource.jsp” โดยวางไว้ที่ “<tomcat server>/webapps/test” โดยในโปรแกรมเขียนไว้ตามนี้
<%@ page import="javax.sql.DataSource" %>
<%@ page import="javax.naming.InitialContext" %>
<%@ page import="javax.naming.Context" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.sql.SQLException" %>
<%
// Obtain a reference to the data source
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
DataSource dataSource = (DataSource)envContext.lookup("jdbc/myDataSource");
// Attempt to acquire a connection from the data source
Connection connection = null;
try {
connection = dataSource.getConnection();
out.println("Connection successful!");
} catch (SQLException e) {
out.println("Connection failed: " + e.getMessage());
} finally {
if (connection != null) {
try {
connection.close();
} catch (SQLException e) {
// Handle the exception, if necessary
}
}
}
%>
จากนั้นก็ทำการทดสอบโดยเรียกไปที่หน้าที่เราสร้างขึ้น
ถ้าสามารถ connect ได้จะได้ output เป็น “Connect successful!” ตามภาพครับ 😄
อีกหนึ่งวิธีที่น่าจะเหมาะกับทาง developer ที่จะทำให้สามารถจัดการตัว datasource ได้เอง แล้วยังไม่มี downtime เวลาเพิ่ม หรือแก้ไข datasource อีกด้วย
Developers can create and configure their own data sources within their application and deploy them together with their code in Apache Tomcat. This approach allows developers to have more control over the data source configuration specific to their application.
To create a data source within the application, developers typically define the data source configuration in a configuration file specific to their application. This file can be an XML file, a properties file, or any other format suitable for their needs.
Here’s a general outline of the steps involved:
1. Within the application’s codebase, create a configuration file to define the data source properties. For example, you could create a datasource.xml file:
<datasource>
<url>jdbc:mysql://localhost/database</url>
<username>user</username>
<password>password</password>
…
</datasource>
The file should contain the necessary properties such as the JDBC URL, username, password, and any additional configuration specific to the data source.
2. In your application code, load and parse the configuration file to retrieve the data source properties.
3. Use the retrieved properties to programmatically create and configure the data source instance within your application code. You’ll typically need to instantiate a data source object provided by your JDBC driver and set its properties accordingly.
4. Ensure that the data source instance is made available to your application’s code for establishing database connections. This can be achieved by storing the data source instance in a context object, dependency injection, or any other mechanism suitable for your application’s framework or architecture.
5. Package the data source configuration file and your application code together into a deployable package, such as a WAR file.
6. Deploy the packaged application to Apache Tomcat, and the data source will be available within the application.
By creating their own data sources within the application, developers can have greater control over the data source configuration, customization, and versioning specific to their application’s needs. This approach also allows for easier application deployment and portability since the data source configuration is packaged along with the application code.
Note that this approach may require coordination and collaboration with the system administrator or deployment team to ensure that the necessary infrastructure, such as database server access and connection pooling, is in place on the deployment environment.