![]() |
![]() |
![]() |
Apache Maven & Failsafe |
![]() |
Apache Tomcat |
Disclosure
Before you start reading further: Keep in mind that this is a work in progress, and although i'm a "Systems&Informatics Engineer" i consider myself a novice programmer. If you have any question you don't see answered here, please leave your comment on the section below to help me improve this article.Also, you should be aware that this post was a result of a lot of work. If you like it, please feel free to show your appreciation by offering me a cup of coffee through paypal and/or add value to this article by sharing your knowledge.
Today we will be picking up the previous "Simple Document/Wrapped SOAP JAX-WS WebServices Tutorial with Maven+Tomcat+Failsafe" project and we will add MySQL database access support.
You can start by downloading the project (if you haven't done so already) and run the 'clean verify' targets just to make sure everything is OK and ready to go.
Chosing the way to access the database
Before going into implementation details, we should be aware of our options to connect to a database.The easiest way would be to hard-code your connection details in Java. This solution stores all the connection details (including username/password) and because of that you would end up with a solution that requires more time and effort to maintain as you would need to re-build and deploy the web application.
To build a web application in a portable and flexible way we should use external configuration files and libraries that allows us to connect to whatever database we would like.
Keeping that in mind, how do we configure/build/deploy/test our web application using Maven?
Configuring the Web Application
The best way to configure your webapp is through 'Resource Injection' using Annotations like @Resource. For example:1 2 3 4 5 6 | public class DBConnectionFactory { @Resource (name= "myDB" ) private static DataSource ds; public static Connection getConnection() throws SQLException { return ds.getConnection(); } } |
1 2 3 4 5 6 7 8 | < web-app > ... < listener > < listener-class >ext.domain.pkg.DBConnectionFactory</ listener-class > ... </ listener > ... </ web-app > |
If this method fails, you can still use the alternative style of looking into the JNDI configuration as stated in the mkyong blog:
1 2 3 4 5 6 7 8 9 10 | private static javax.sql.DataSource ds; static { try { String resourceName = "mydb" ds = (DataSource) new InitialContext().lookup( "java:comp/env/" + resourceName); } catch (NamingException e) { e.printStackTrace(); } } |
Context Configuration
The above code trys to fetch whatever resource is defined under "java:comp/env/" named "mydb". This can already be defined in a production server using server.xml (for an all apps context configuration), but can also be defined as a "per app context configuration". To use the latter type of configuration, we need to add a META-INF folder in our webapp folder and add a context.xml to that folder like so:1 2 3 4 5 6 | <? xml version = '1.0' encoding = 'UTF-8' ?> < Context > < Resource name = "mydb" type = "javax.sql.DataSource" username = "root" password = "" driverClassName = "com.mysql.jdbc.Driver" </ Context > |
Configuring the Maven build
And now for the trifecta we have to configure Maven to include the MySQL Connector/J library in the WAR file. Change your current Maven pom.xml and add the following to the dependencies configuration:1 2 3 4 5 6 7 8 9 | < dependencies > ... < dependency > < groupId >mysql</ groupId > < artifactId >mysql-connector-java</ artifactId > < version >5.1.25</ version > </ dependency > ... </ dependencies > |
That's it! If you followed this post correctly you should now be able to get a Connection from the DataSource factory using:
1 | DBConnectionFactory.getConnection(); |
Happy Coding!