SolveItproblems

SolveITProblems is a project of QFormat.net intellectual club

SolveITProblems это проект интеллектуального клуба QFormat.net

понедельник, 22 октября 2012 г.

Create Java web-application with Database Pooling using maven in netbeans

Hi, this article is for guys who knows Java, works with netbeans, understand maven, and needs some examples :)
There are 8 steps to create an java web-application with maven, using netbeans, which will be connected to the database via connection pooling. Also we will show how to use jetty server with maven.




1) Create Maven web-project in netbeans. Yeah, new project->maven->web application
2) If you have problems with memory, its recomended to use not embedded glassfish or tomcat but jetty.
[dont worry after some steps we will show you, how to do the same in tomcat, you'll see that our application will not be ]
You need tom make modifies in pom.xml file:


<build>
<plugins>
<plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>maven-jetty-plugin</artifactId>
                <version>6.1.0pre3</version>
                <configuration>
                    <scanIntervalSeconds>5</scanIntervalSeconds>
                </configuration>
            </plugin>

           
...
3) Before run you should create a "WEB-INF" directory under the "Web Pages"
4) And standard deployment descriptor (web.xml) under this directory
[right click on project->Custom->Goals->jetty:run will give a breath to your project]
[if you want to use servlet, you need to define them at web.xml its not a tomcat]
5) So lets go to the database part. First you need to add jdbc driver (again to the pom.xml). In case of oracle you need:


 <dependencies>      
          <dependency>
            <groupId>ojdbc</groupId>
            <artifactId>ojdbc</artifactId>
            <version>14</version>
        </dependency>

       
...
6)We dont need just connection to the database, we need connection pooling
 
   
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1</version>
        </dependency>
     
Add this lib to use the ability of connection pooling

Then, as we ue jetty, we need to add datasource for jetty and datasource definition to our application.

6.1)Adding the datasource to jetty:

Create file jetty-env.xml under the WEB-INF and write there:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<Configure  class="org.mortbay.jetty.webapp.WebAppContext">

<New id="DSTest" class="org.mortbay.jetty.plus.naming.Resource">  
    <Arg>jdbc/DSTest</Arg>
    <Arg>
     <New class="com.mchange.v2.c3p0.ComboPooledDataSource">
                 <Set name="driverClass">oracle.jdbc.driver.OracleDriver</Set>
                 <Set name="jdbcUrl">jdbc:oracle:thin:@server.com:1522:DBNAME</Set>
                 <Set name="user">username</Set>
                 <Set name="password">password</Set>
     </New>
    </Arg>
   </New>
   </Configure>


6.2) Adding datasource to our application at web.xml


...
    <resource-ref>
        <res-ref-name>jdbc/DSTest</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
        <res-sharing-scope>Shareable</res-sharing-scope>
    </resource-ref>
</web-app>



6.3) Write simple code for checking the datasource

You can add at your servlet:

InitialContext ic = new InitialContext();
                DataSource ds = (DataSource) ic.lookup("java:comp/env/jdbc/DSTest");
                Connection con = ds.getConnection();
                Statement st = con.createStatement();
                ResultSet rs = st.executeQuery("select * from our_table ");

7) Our web-application is ready. You can build it (right click on app and Build) and run the app (as we do it at step 4)
8) Hm, but what you should do, if you are using tomcat?
Here is the solution:
As the tomcat doesnt understand the jetty-env.xml we need to create a META-INF directory, and context.xml file and write there the following:



<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" path="/">
  <Resource MaxActive="20" MaxIdle="5" MaxWait="200" auth="Container" driverClassName="oracle.jdbc.driver.OracleDriver" name="jdbc/DSTest" password="password" removeAbandonTimeout="120" removeAbandoned="true" type="javax.sql.DataSource" url="jdbc:oracle:thin:@server.com:1522:DBNAME" username="username"/>

</Context>
   

1 комментарий: