PageRenderTime 25ms CodeModel.GetById 33ms RepoModel.GetById 1ms app.codeStats 0ms

/intermediate/stored-procedures-oracle/README.md

https://gitlab.com/tpayseno/spring-integration-samples
Markdown | 251 lines | 182 code | 69 blank | 0 comment | 0 complexity | 25e9da34bee88f230de7e465f49a1fe8 MD5 | raw file
  1. Spring Integration - Stored Procedure Example - Oracle
  2. ======================================================
  3. ## Overview
  4. This sample application illustrates the usage of the *Spring Integration* Stored Procedure components using an Oracle database as a backend.
  5. Actually 2 samples are provided:
  6. * **Sample 1** Capitalizes Strings
  7. * **Sample 2** Retrieves Coffee Data
  8. - This is sample is similar to the [Stored Procedure Sample for PostgreSql][]
  9. ## Running the Sample
  10. ### Pre-requisites
  11. This sample was tested against: **Oracle Database Express Edition 11g Release 2** (which can be downloaded and used for free).
  12. Nevertheless, the example should work with other versions as well.
  13. - Access to a Oracle or Oracle XE database instance
  14. - Install Oracle JDBC Driver to your local Maven repository (~/.m2)
  15. ### JDBC Driver Installation for Oracle
  16. - Go to [http://www.oracle.com/technetwork/indexes/downloads/index.html](http://www.oracle.com/technetwork/indexes/downloads/index.html)
  17. - Under "JDBC Drivers", download the appropriate driver relevant to your Oracle and JDK version (This sample was tested using "Oracle Database 11g Release 2 JDBC Drivers")
  18. - Once downloaded, install the driver to your local Maven repository:
  19. $ mvn install:install-file -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.2.0.3 -Dpackaging=jar -Dfile=~/dev/ojdbc6.jar -DgeneratePom=true
  20. - Now you can uncomment the `ojdbc6` dependency in the build.gradle file for `stored-procedures-oracle` project.
  21. After that you can run the sample application using [Gradle Application Plugin](http://www.gradle.org/docs/current/userguide/application_plugin.html):
  22. $ gradlew :stored-procedures-oracle:run
  23. ### Common Oracle Setup
  24. #### Create Tablespace
  25. ```SQL
  26. CREATE TABLESPACE procedure_test
  27. DATAFILE 'c:/data/procedure_test.dbf'
  28. SIZE 10M
  29. AUTOEXTEND ON NEXT 10M
  30. MAXSIZE 100M;
  31. ```
  32. #### Create User
  33. ```SQL
  34. CREATE USER storedproc
  35. IDENTIFIED BY storedproc
  36. DEFAULT TABLESPACE procedure_test
  37. TEMPORARY TABLESPACE temp;
  38. ```
  39. #### Grant Rights
  40. ```SQL
  41. GRANT CREATE SESSION, CREATE TABLE, CREATE VIEW, CREATE SEQUENCE, CREATE PROCEDURE TO storedproc;
  42. ALTER USER storedproc QUOTA unlimited ON procedure_test;
  43. ```
  44. ### Setting up the Spring Application Context
  45. You may have to update the Oracle DB properties in:
  46. /src/main/resources/META-INF/spring/integration/spring-integration-context.xml
  47. ```XML
  48. <bean id="dataSource" class="oracle.jdbc.pool.OracleDataSource" destroy-method="close">
  49. <property name="connectionCachingEnabled" value="true" />
  50. <property name="URL" value="jdbc:oracle:thin:@//localhost:1521/XE" />
  51. <property name="password" value="storedproc" />
  52. <property name="user" value="storedproc" />
  53. <property name="connectionCacheProperties">
  54. <props merge="default">
  55. <prop key="MinLimit">3</prop>
  56. <prop key="MaxLimit">20</prop>
  57. </props>
  58. </property>
  59. </bean>
  60. ```
  61. ## Running Sample 1 - Capitalizes Strings
  62. This example provides a simple example using the stored procedure outbound gateway
  63. adapter. This example will call an Oracle Stored Procedure as well as an Oracle Function using the StoredProc Outbound Gateway.
  64. ### Creating the Stored Procedure
  65. ```SQL
  66. create or replace
  67. PROCEDURE CAPITALIZE_STRING(inoutString IN OUT VARCHAR) IS
  68. BEGIN
  69. SELECT upper(inoutString) INTO inoutString from dual ;
  70. END;
  71. ```
  72. ### Creating the Stored Function
  73. ```SQL
  74. create or replace
  75. FUNCTION GET_COOL_NUMBER
  76. RETURN NUMBER
  77. IS cool_number NUMBER(11,2);
  78. BEGIN
  79. cool_number := 12345;
  80. RETURN cool_number;
  81. END;
  82. ```
  83. ### Execute the Sample
  84. * running the "Main" class from within STS (Right-click on Main class --> Run As --> Java Application)
  85. * or from the command line:
  86. $ gradlew :stored-procedures-oracle:run
  87. You should see the following output:
  88. 16:05:19.556 INFO [main][org.springframework.integration.samples.storedprocedure.Main]
  89. =========================================================
  90. Welcome to Spring Integration's
  91. Stored Procedure/Function Sample for Oracle
  92. For more information please visit:
  93. http://www.springsource.org/spring-integration
  94. =========================================================
  95. Please enter a choice and press <enter>:
  96. 1. Execute Sample 1 (Capitalize String)
  97. 2. Execute Sample 2 (Coffee Service)
  98. q. Quit the application
  99. Select **Opion 1**.
  100. =========================================================
  101. Please press 'q + Enter' to quit the application.
  102. =========================================================
  103. Please enter a string and press <enter>: hello world
  104. Converting String to Uppcase using Stored Procedure...
  105. Retrieving Numeric value via Sql Function...
  106. Converted 'hello world' - End Result: 'HELLO WORLD_12345'.
  107. When you enter a text, the text will be converted into upper-case using the Oracle Stored Procedure named `CAPITALIZE_STRING`. Afterwards, the String is concatenated with the result from calling the Oracle Stored Function `GET_COOL_NUMBER`.
  108. ## Running Sample 2 - Coffee Service
  109. ### Create Table COFFEE_BEVERAGES
  110. ```SQL
  111. CREATE TABLE "COFFEE_BEVERAGES"(
  112. "ID" NUMBER(10,0) NOT NULL,
  113. "COFFEE_NAME" VARCHAR2(50 CHAR) NOT NULL,
  114. "COFFEE_DESCRIPTION" VARCHAR2(500 CHAR) NOT NULL,
  115. CONSTRAINT "COFFEE_BEVERAGES_PK" PRIMARY KEY ("ID"));
  116. ```
  117. ### Add Sample Data to Table COFFEE_BEVERAGES
  118. ```SQL
  119. REM INSERTING into COFFEE_BEVERAGES
  120. SET DEFINE OFF;
  121. Insert into COFFEE_BEVERAGES (ID,COFFEE_NAME,COFFEE_DESCRIPTION) values (1,'Espresso','Espressos keep developers going in the morning. There are never enough of them.');
  122. Insert into COFFEE_BEVERAGES (ID,COFFEE_NAME,COFFEE_DESCRIPTION) values (2,'Cappuccino','For the finer moments. Wrap your espresso in a tasty layer of foam.');
  123. Insert into COFFEE_BEVERAGES (ID,COFFEE_NAME,COFFEE_DESCRIPTION) values (3,'Mocha','Mmmmh, chocolate.');
  124. Insert into COFFEE_BEVERAGES (ID,COFFEE_NAME,COFFEE_DESCRIPTION) values (4,'Latte','If you are more into milk than into foam.');
  125. ```
  126. ### Creating the Stored Functions
  127. Please create the following Stored Functions:
  128. #### Find All Coffee Beverages
  129. ```SQL
  130. create or replace
  131. package types
  132. as
  133. type cursorType is ref cursor;
  134. end;
  135. ```
  136. ```SQL
  137. create or replace
  138. FUNCTION find_all_coffee_beverages return types.cursortype
  139. AS
  140. l_cursor types.cursorType;
  141. BEGIN
  142. OPEN l_cursor FOR SELECT "ID", "COFFEE_NAME", "COFFEE_DESCRIPTION" FROM "COFFEE_BEVERAGES";
  143. RETURN l_cursor;
  144. END;
  145. ```
  146. #### Find Specific Coffee Beverage
  147. ```SQL
  148. create or replace
  149. FUNCTION find_coffee(coffee_id IN integer)
  150. RETURN VARCHAR2 is description VARCHAR2(1000);
  151. begin
  152. SELECT COFFEE_DESCRIPTION into description from COFFEE_BEVERAGES where ID=coffee_id;
  153. return description;
  154. end;
  155. ```
  156. ### Execute the Sample
  157. * running the "Main" class from within STS (Right-click on Main class --> Run As --> Java Application)
  158. * or from the command line:
  159. $ gradlew :stored-procedures-oracle:run
  160. You should see the following output:
  161. 16:05:19.556 INFO [main][org.springframework.integration.samples.storedprocedure.Main]
  162. =========================================================
  163. Welcome to Spring Integration's
  164. Stored Procedure/Function Sample for Oracle
  165. For more information please visit:
  166. http://www.springsource.org/spring-integration
  167. =========================================================
  168. Please enter a choice and press <enter>:
  169. 1. Execute Sample 1 (Capitalize String)
  170. 2. Execute Sample 2 (Coffee Service)
  171. q. Quit the application
  172. Select **Opion 2**.
  173. This should result in the following output:
  174. * Please enter 'list' and press <enter> to get a list of coffees.
  175. * Enter a coffee id, e.g. '1' and press <enter> to get a description.
  176. * Please press 'q + Enter' to quit the application.
  177. This sample also periodically polls the Oracle database using a **Stored Procedure Inbound Channel Adapter**:
  178. 16:06:46.669 INFO [task-scheduler-1][org.springframework.integration.handler.LoggingHandler] [Payload=[CoffeeBeverage [id=1,...
  179. --------------------------------------------------------------------------------
  180. For help please take a look at the Spring Integration documentation:
  181. http://www.springsource.org/spring-integration
  182. [Stored Procedure Sample for PostgreSql]: https://github.com/ghillert/spring-integration-samples/tree/master/intermediate/stored-procedures-postgresql