Saturday, July 6, 2013

Can you explain how service builder creates tables, indexes, sequences from service.xml?

when there is no data source attribute defined in entity tag, service builder creates *.sql files in sql folder with SQL scripts inside

tables.sql
indexes.sql
sequences.sql

i.e.
Ex:
    <entity  name="Employee" local-service="true" remote-service="true" >


if you define  data-source for entity, service builder creates empty sql files. you are responsible for creating required tables in the target data sources.


when we deploy portlet from Liferay by default service builder components creates tables in default liferay database. So when we specify data-source in entity tag  *.sql are empty by purpose.


This is how Service Builder components works


ServiceComponentLocalServiceImpl.java:

    public void upgradeDB(
            ClassLoader classLoader, String buildNamespace, long buildNumber,
            boolean buildAutoUpgrade, ServiceComponent previousServiceComponent,
            String tablesSQL, String sequencesSQL, String indexesSQL)
        throws Exception {

        DB db = DBFactoryUtil.getDB();

        if (previousServiceComponent == null) {
            if (_log.isInfoEnabled()) {
                _log.info("Running " + buildNamespace + " SQL scripts");
            }

            db.runSQLTemplateString(tablesSQL, true, false);
            db.runSQLTemplateString(sequencesSQL, true, false);
            db.runSQLTemplateString(indexesSQL, true, false);
        }


It try to get database from DBFactoryUtil.java

DBFactoryUtil ->  DBFactoryImpl  -> MySQLDB -> DataAccess -> InfrastructureUtil:getDataSource 


Spring injects required data soruces to  InfrastructureUtil.java

infrastructure-spring.xml

    <bean id="liferayDataSource" class="org.springframework.jdbc.datasource.LazyConnectionDataSourceProxy">
        <property name="targetDataSource">
            <bean class="com.liferay.portal.dao.jdbc.util.DataSourceFactoryBean">
                <property name="propertyPrefix" value="jdbc.default." />
            </bean>
        </property>
    </bean>
    <bean id="mailSession" class="com.liferay.mail.util.MailSessionFactoryBean">
        <property name="propertyPrefix" value="mail.session." />
    </bean>
    <bean id="com.liferay.portal.kernel.util.InfrastructureUtil" class="com.liferay.portal.kernel.util.InfrastructureUtil">
        <property name="dataSource" ref="liferayDataSource" />
        <property name="mailSession" ref="mailSession" />
        <property name="transactionManager" ref="liferayTransactionManager" />
    </bean>


it uses liferayDatasource. That's why tables created by default in liferay database.