littlecowk35 / eic-bao-tri-he-thong-oss

Automatically exported from code.google.com/p/eic-bao-tri-he-thong-oss
0 stars 0 forks source link

Use tomcat Datasource for Oracle connection #8

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Replace web app datasource with tomcat datasource

Original issue reported on code.google.com by ntanh...@gmail.com on 18 Sep 2014 at 1:55

GoogleCodeExporter commented 9 years ago
https://www.liferay.com/community/wiki/-/wiki/Main/Add+Datasource+to+Portlet
Add the following line to tomcat-7.0.42/conf/context.xml

<Resource name="jdbc/oracle"
     auth="Container"
     type="javax.sql.DataSource"
     maxActive="10"
     maxIdle="30"
     maxWait="10000"
     username="htlyktt"
     password="admin"
     driverClassName="oracle.jdbc.driver.OracleDriver"
     url="jdbc:oracle:thin:@localhost:1521:xe"/>

Original comment by ntanh...@gmail.com on 18 Sep 2014 at 2:32

GoogleCodeExporter commented 9 years ago
Using this code to get datasource
public class ConnectionDatasource {
    public static DataSource datasource  = null;
    static {
        String DATASOURCE_CONTEXT = "java:comp/env/jdbc/oracle";
         try {
             Context initialContext = new InitialContext();
             datasource = (DataSource)initialContext.lookup(DATASOURCE_CONTEXT);
             if (datasource != null) {
             //result = datasource.getConnection();
                 System.out.println("Lookup datasource.");
             //Statement stmt = result.createStatement();
             }else{
                 System.out.println("Can not find datasource.");
             }
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
    }

}

Original comment by ntanh...@gmail.com on 18 Sep 2014 at 2:33