LSPS documentation logo
LSPS Documentation
Accessing Data from other Data Sources

To access data from external resources, we will set up connection to the datasource, create an Entity and manage it via an EJB. This will be typically helpful when you have an existing database or your database is populated by an external system, and you want to obtain and manipulate the data from the code of your LSPS Application.

Prerequisites

Make sure you have the following ready:

  • You have set up a database or you have access to a database. It is a good idea to connect to the database with an SQL client.
  • You have generated a LSPS Application with the related resources in PDS.
    generatingApp.png

Configuring Data Source

To configure a data source, so it is accessible from the SDK Embedded Server, and you can use its entities do the following:

  1. In <YOUR_APP>-embedded/conf/conf/openejb.xml, define the data source.

    For example:

    ...
    JdbcUrl jdbc:h2:tcp://localhost/h2/h2;MVCC=TRUE;LOCK_TIMEOUT=60000
    Username lsps
    Password lsps
    </Resource><!-- adding this Resource tag:-->
    <Resource id="jdbc/USERS_DS" type="javax.sql.DataSource">
    JdbcDriver com.mysql.cj.jdbc.Driver
    JdbcUrl jdbc:mysql://localhost:3306/training_users;
    Username root
    Password root </Resource>
  2. Restart the SDK Embedded Server.

Creating the Entity

  1. Create a new package in the ejb project with the entity class.
    @Entity
    @Table(name = "ORDERS_USER")
    public class User {
      @Id
      private Integer id;
      @Column(name = "FIRST_NAME")
      private String firstName;
      public Integer getId() { return id; }
      public String getFirstName() {
      return firstName;
      }
    }
    
  2. Create <YOUR_AP>-ejb/src/main/resources/resources/META-INF/persistence.xml and define the persistence unit with the external data source.
    <?xml version="1.0" encoding="UTF-8"?>
      <persistence xmlns="http://java.sun.com/xml/ns/persistence"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
                   version="2.0">
        <persistence-unit name="<UNIT_NAME>" transaction-type="JTA">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <jta-data-source><DATASOURCE_ID></jta-data-source>
        <mapping-file>META-INF/<PROJECT_NAME>-entities.xml</mapping-file>
        <validation-mode>NONE</validation-mode>
        <properties>
          <property name="hibernate.cache.region.factory_class" value="org.hibernate.cache.ehcache.EhCacheRegionFactory" />
          <property name="net.sf.ehcache.configurationResourceName" value="META-INF/lsps-ehcache.xml" />
          <!-- JBoss specific parameters -->
          <property name="jboss.as.jpa.providerModule" value="application" />
          <property name="jboss.as.jpa.adapterClass" value="com.whitestein.lsps.common.hibernate.LSPSPersistenceProviderAdaptor" />
        </properties>
      </persistence-unit>
    </persistence>
    
  3. Create the mapping file for the persistence unit.
    <?xml version="1.0" encoding="UTF-8" ?>
      <entity-mappings xmlns="http://java.sun.com/xml/ns/persistence/orm"
                       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                       xsi:schemaLocation="http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
                      version="2.0">
      <entity class="org.eko.orderusersapp.entity.User" />
    </entity-mappings>
    
  4. Create the ehcache configuration file for the persistence unit.
    <?xml version="1.0" encoding="UTF-8"?>
      <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
               xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
               name="<UNIT_NAME>" updateCheck="false" monitoring="off" dynamicConfig="false">
      <cacheManagerPeerProviderFactory class="com.whitestein.lsps.common.ehcache.JmsCacheManagerPeerProviderFactory"/>
      <defaultCache eternal="true" maxElementsInMemory="0" overflowToDisk="false" >
        <cacheEventListenerFactory class="com.whitestein.lsps.common.ehcache.JmsCacheReplicatorFactory"/>
      </defaultCache>
      <cache name="org.hibernate.cache.internal.StandardQueryCache" maxBytesLocalHeap="10000000" eternal="true" overflowToDisk="false">
        <cacheEventListenerFactory class="com.whitestein.lsps.common.ehcache.JmsCacheReplicatorFactory"/>
      </cache>
      <cache name="org.hibernate.cache.spi.UpdateTimestampsCache" maxElementsInMemory="1000" eternal="true" overflowToDisk="false">
        <cacheEventListenerFactory class="com.whitestein.lsps.common.ehcache.JmsCacheReplicatorFactory"/>
      </cache>
    </ehcache>
    

Registering EJB

If you want to use the entity via an EJB in LSPS modules, do the following:

  1. Create the bean class with an entity manager.
    @Stateless
    @PermitAll
    @Interceptors({ LspsFunctionInterceptor.class })
    public class UserBean {
        @PersistenceContext(unitName = "user-unit")
        private EntityManager em;
        public String getUsers(ExecutionContext context) {
            User user = em.find(User.class, 1);
            System.out.println(user.getFirstName());
            return user.getFirstName();
        }
    }
    
  2. Register the EJB in the ComponentServiceBean class:
    @EJB
    private UserBean userBean;
    @Override
    protected void registerCustomComponents() {
        register(userBean, UserBean.class);
    }
    

Calling EJB Method from LSPS

Create a function definition file with a function that will use the keyword native to call the EJB method.