Junos Space SDK > Developer Guides > Junos Space Application Developer Guide > Programming with the Junos Space SDK > Using the Junos Space SDK Plug-in

Connecting to External Databases

The Junos Space Platform provides a pre-installed MySQL database for your applications. Alternatively, the Junos Space application can be connected to external relational (MySQL only) and non-relational databases.

The steps required to connect a Junos Space application to an external database depend on whether the database is a relational or non-relational.

Connecting to an External Relational Database

To develop an application that can connect to a remotely-hosted database, you need to configure the DataSource file. The DataSource configuration file names end with the suffix -ds.xml to be recognized by the JCA deployer.

For more information, refer to the JBoss DataSource configuration file.

  1. To configure the DataSource file provide the following information in the DataSource configuration file (ds.xml).
  2. Note: The code snippet example below shows the DataSource configuration file (ds.xml) for MySQL database.

    <?xml version="1.0" encoding="UTF-8"?>
    <datasources>
     <ha-xa-datasource>
        <jndi-name>HelloWorldDS</jndi-name>
        <xa-datasource-class>com.mysql.jdbc.jdbc2.optional.MysqlXADataSource</xa-datasource-class>
        <!-- Note: "&" has to be used instead of "&" for parameters -->
        <xa-datasource-property name="URL">jdbc:mysql://127.0.0.1:3306/NewDatabase?autoReconnect=true
        &autoReconnectForPools=true&includeInnodbStatusInDeadlockExceptions=true
        &dumpQueriesOnException=true</xa-datasource-property>
        <url-property>URL</url-property>
        <url-delimiter>|</url-delimiter>
        <user-name>jboss</user-name>
        <password>netscreen</password>
    
        <transaction-isolation>TRANSACTION_READ_COMMITTED</transaction-isolation>
        <no-tx-separate-pools/>
        <!-- This disables transaction interleaving (which BTW, most DB vendors don't support) -->
        <track-connection-by-tx/>
        <isSameRM-override-value>false</isSameRM-override-value>
    
    
        <!--pooling parameters-->
        <min-pool-size>5</min-pool-size>
        <max-pool-size>200</max-pool-size>
        <blocking-timeout-millis>5000</blocking-timeout-millis>
        <idle-timeout-minutes>15</idle-timeout-minutes>
        <!-- maximum of 5 minutes for queries -->
        <query-timeout>1800</query-timeout>
        <prepared-statement-cache-size>20</prepared-statement-cache-size>
        <set-tx-query-timeout>true</set-tx-query-timeout>
    
    
        <!-- If you supply the usr/pw from a JAAS login module -->
        <security-domain/>
        <check-valid-connection-sql>select current_date()</check-valid-connection-sql>
        <exception-sorter-class-name>
            com.mysql.jdbc.integration.jboss.ExtendedMysqlExceptionSorter
        </exception-sorter-class-name>
        <valid-connection-checker-class-name>
            com.mysql.jdbc.integration.jboss.MysqlValidConnectionChecker
        </valid-connection-checker-class-name>
      </ha-xa-datasource>
    </datasources>
    

  3. Modify the persistence.xml file in the src/META-INF directory of the EJB project.
  4. Note: If your Junos Space application uses multiple external databases, then you must make multiple persistence-unit entries in the persistence.xml file.

    <?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_1_0.xsd"
        version="1.0">
    <persistence-unit name="HelloWorldDBUnit">
    	<jta-data-source>java:/HelloWorldDS</jta-data-source>	
    	<properties>
    		<property name="hibernate.hbm2ddl.auto" value="update"/>
    		<property name="hibernate.archive.autodection" value="class, hbm"/>
    		<property name="hibernate.show_sql" value="false"/>
    		<property name="hibernate.dialect" value="org.hibernate.dialect.MySQLInnoDBDialect"/>
    		<property name="hibernate.statement_cache.size" value="20"/>
    	</properties>
    </persistence-unit>
    </persistence>
    

  5. From File > Junos Space, launch the Build/Deploy Junos Space Application or Build, Deploy and Package For Distribution window. In either window, clear the Create Application Database checkbox. This option creates the default MySQL database internally in the Junos Space platform.
  6. Note: To connect to an external database, ensure that this checkbox is cleared.

  7. Instructions:

Connecting to an External Non-Relational Database

The JBoss application server does not support connections to a non-relational database.

  1. Obtain the JDBC connectivity drivers of all external databases.
  2. Copy the JDBC connector/driver to the EarContent/lib directory of the EAR project.
  3. From File > Junos Space, launch the Build/Deploy Junos Space Application or the Build, Deploy and Package For Distribution window. In either window, clear the Create Application Database checkbox. This option creates the default MySQL database internally in the Junos Space platform.
  4. Note: To connect to an external database, ensure that this checkbox is cleared.

If you are using the non-relational database only, then, remove the files associated with the Relational Database. You can use the steps below to remove those files.

  1. In the Project Explorer tab, right-click your EJB project folder and select the Properties option.


  2.  
  3. Select Project Facets and clear the JPA checkbox.