Message Boards

question about add external ojdbc jar in liferay 7.4 module

Scarletake Bwi, modified 2 Years ago.

question about add external ojdbc jar in liferay 7.4 module

Expert Posts: 326 Join Date: 12/20/10 Recent Posts

hi 

base on article osgi-module-dependencies

i download ojdbc8-21.3.0.0.jar, and put it in liferay-ce-portal-7.4.2-ga3\tomcat-9.0.43\lib

i create a mvc-module and modify the build.gradle

runtime group: 'com.oracle.database.jdbc', name: 'ojdbc8', version: '21.3.0.0'

i try use simple jdbc in my portlet, such like:

Class.forName(driver);
DriverManager.getConnection(url, username, password);

image.gif
 

it should work, but it doesn't.

supposly, after i put jar in tomcat/lib, whole server, i mean, include osgi, will get the jar. i do not even modify my build.gradle like

runtime group: 'com.oracle.database.jdbc', name: 'ojdbc8', version: '21.3.0.0'

 

can anyone help, thank you very very much in advance.

Another very strange thing, i add a main in my portlet, and debug as Java Application


 

it should not works, but it got connection and got resule.

i am sure it use the jar in "Project and External Dependencies", because after i remove it from build.gradle and sync, the jar in "Project and External Dependencies" is gone, and it cannot work.

thumbnail
Olaf Kock, modified 2 Years ago.

RE: question about add external ojdbc jar in liferay 7.4 module

Liferay Legend Posts: 6403 Join Date: 9/23/08 Recent Posts

supposly, after i put jar in tomcat/lib, whole server, i mean, include osgi, will get the jar. i do not even modify my build.gradle like

You're wrong about this: While tomcat makes those jars available to web application, there's no mandate that the classloader used by the webapp - specifically in the case of OSGi - has to make them available to applications. OSGi is there for explicit​​​​​​​ dependency management.

That being said, check module.framework.system.packages.extra from portal.properties

Also, to be friendly for indexing, screenreaders, responsive UIs and mobile devices, code (or any text) should never be posted as image. Last time I've checked, the java compiler didn't accept gif, png or jpg as input, so it's hard to reproduce anything from your post.

 

 

Scarletake Bwi, modified 2 Years ago.

RE: RE: question about add external ojdbc jar in liferay 7.4 module

Expert Posts: 326 Join Date: 12/20/10 Recent Posts

hi Olaf

thank you for reply, 

yes, i had checked module.framework.system.packages.extra , "oracle.jdbc" is already in the list as default.

from tomcat-9.0.43\webapps\ROOT\WEB-INF\shielded-container-libportal-impl.jar\portal.properties

    module.framework.system.packages.extra=\
        com.ibm.crypto.provider,\
        com.ibm.db2.jcc,\
        com.microsoft.sqlserver.jdbc,\
        com.mysql.cj.jdbc,\
        com.mysql.jdbc,\
        com.p6spy.engine.spy,\
        com.sun.security.auth.module,\
        com.sybase.jdbc4.jdbc,\
        oracle.jdbc,\
        org.postgresql,\
        org.hsqldb.jdbc,\
        org.mariadb.jdbc,\
        sun.misc,\
        sun.net.util,\
        sun.security.provider,\
        \
        #
        # WebSocket Support
        #
        \
        com.ibm.websphere.wsoc,\
        io.undertow.websockets.jsr,\
        javax.websocket,\
        javax.websocket.server,\
        org.apache.tomcat.websocket.server,\
        weblogic.websocket.tyrus

 

i am sorry, i thought image will make it mare clear

my portlet

package com.ci.zt.portlet;

import com.ci.zt.constants.ZTestMVCPortletKeys;
import com.liferay.portal.kernel.portlet.bridges.mvc.MVCPortlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.portlet.Portlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.osgi.service.component.annotations.Component;

/**
 * @author vic.chen
 */
@Component(
    immediate = true,
    property = {
        "com.liferay.portlet.display-category=category.sample",
        "com.liferay.portlet.header-portlet-css=/css/main.css",
        "com.liferay.portlet.instanceable=true",
        "javax.portlet.display-name=ZTestMVC",
        "javax.portlet.init-param.template-path=/",
        "javax.portlet.init-param.view-template=/view.jsp",
        "javax.portlet.name=" + ZTestMVCPortletKeys.ZTESTMVC,
        "javax.portlet.resource-bundle=content.Language",
        "javax.portlet.security-role-ref=power-user,user"
    },
    service = Portlet.class
)
public class ZTestMVCPortlet extends MVCPortlet {
    
    @Override
    public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
            throws IOException, PortletException {
        // TODO Auto-generated method stub
        super.doView(renderRequest, renderResponse);
        this.test();
        
    }

    
    public void test() {
        Statement stmt = null;
        ResultSet rs = null;
        try {
          Class.forName("oracle.jdbc.OracleDriver");
            Connection con = DriverManager.getConnection("jdbc:oracle:thin:@testhost:1521:test", "username", "password");
            stmt = con.createStatement();
            rs = stmt.executeQuery(MATERIAL_INFO("100-72-5000-00"));
            while (rs.next()) {
                System.out.println(rs.getString("CHILD_PART_NUMBER"));
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    public static String MATERIAL_INFO(String masterPartNumber) {
        StringBuffer sql = new StringBuffer();
        sql.append("SELECT ");
        sql.append(" ITEM.ITEM_NUMBER AS CHILD_PART_NUMBER ");
        sql.append("FROM AGILE.ITEM ITEM ");
        sql.append("WHERE 1=1 ");
        sql.append(" AND ITEM.ITEM_NUMBER = '").append(masterPartNumber).append("'");
        return sql.toString();
    }
    
    public static void main(String[] msg) {
        ZTestMVCPortlet plm=new ZTestMVCPortlet();
        plm.test();
    }
    
}

 

my build.gradle

dependencies {
    compileOnly group: "com.liferay.portal", name: "release.portal.api"

    cssBuilder group: "com.liferay", name: "com.liferay.css.builder", version: "3.0.2"
    runtime group: 'com.oracle.database.jdbc', name: 'ojdbc8', version: '21.3.0.0'
    
}

again, thank you for reply

thumbnail
Antonio Musarra, modified 2 Years ago.

RE: question about add external ojdbc jar in liferay 7.4 module

Junior Member Posts: 66 Join Date: 8/9/11 Recent Posts

Hi.

As I wrote in the updated documentation https://github.com/amusarra/liferay-portal-database-all-in-one-support , the jdbc drivers for Liferay 7.4 GA3, must be placed in the webapps/ROOT/WEB-INF/shielded-container-lib folder.  In fact, if you see, inside it you will also find other jdbc drivers.

In any case, it is preferable to use the service builder with the external database (see Liferay documentation).

Antonio.

Scarletake Bwi, modified 2 Years ago.

RE: RE: question about add external ojdbc jar in liferay 7.4 module

Expert Posts: 326 Join Date: 12/20/10 Recent Posts

Hi Antonio

thank you. have a nice day.

i put liferay-portal-database-all-in-one-support-1.2.1.jar in tomcat-9.0.43\webapps\ROOT\WEB-INF\lib, and put ojdbc8.jar in tomcat-9.0.43\webapps\ROOT\WEB-INF\shielded-container-lib

but i still got ClassNotFoundException in Class.forName("oracle.jdbc.OracleDriver");

my final goal is read external data via service builder(if the target is a database). and i tried both spring-bean and datasourceprovider. Unfortunately, they were all unable to succeed for different reasons.

 

Scarletake Bwi, modified 2 Years ago.

RE: question about add external ojdbc jar in liferay 7.4 module (Answer)

Expert Posts: 326 Join Date: 12/20/10 Recent Posts

after i modified service module's bnd.bnd, after "-dsannotations-options: inherit" add follows:

Import-Package: \
    org.springframework.jdbc.datasource,\
    com.liferay.portal.dao.jdbc.spring,\
    *
Liferay-Require-SchemaVersion: 1.0.0
Liferay-Service: true
Liferay-Spring-Context: META-INF/spring

it works.