|
| 1 | +# Oracle Database Local Connection (GraalPy + python-oracledb) Native Demo |
| 2 | + |
| 3 | +This demo shows a small [Flask](https://flask.palletsprojects.com/) application running on **GraalPy** and connecting to a local [Oracle Database Free](https://www.oracle.com/database/free/get-started/) instance using the **python-oracledb** driver. |
| 4 | + |
| 5 | +> The [python-oracledb driver](https://oracle.github.io/python-oracledb/) is the open-source Python module allowing Python programs to connect directly to Oracle Database with no extra libraries needed. |
| 6 | +
|
| 7 | +This project provides a local baseline application for **GraalPy** that can also be compiled into a native standalone executable. |
| 8 | +The application exposes one `GET` HTTP endpoint, runs `select 'Hello from Oracle' from dual`, and returns the result as JSON. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +* [GraalPy 25.0.2](https://www.graalvm.org/python/python-developers/docs/#installation) |
| 13 | +* [GraalVM 25.0.2](https://www.graalvm.org/downloads/) |
| 14 | +* Docker installed and running |
| 15 | + |
| 16 | +## Start Oracle Database Free |
| 17 | + |
| 18 | +1. Log in to Oracle Container Registry: |
| 19 | + ```bash |
| 20 | + docker login container-registry.oracle.com |
| 21 | + ``` |
| 22 | + |
| 23 | + > If you do not want to log in to Oracle Container Registry, you can use the community-maintained Docker Hub image [gvenzl/oracle-free](https://hub.docker.com/r/gvenzl/oracle-free) instead. |
| 24 | +
|
| 25 | +2. Pull the Oracle Database Free image: |
| 26 | + ```bash |
| 27 | + docker pull container-registry.oracle.com/database/free:latest |
| 28 | + ``` |
| 29 | + |
| 30 | +3. Start the database: |
| 31 | + ```bash |
| 32 | + docker run -d \ |
| 33 | + --name oracle-free \ |
| 34 | + -p 1521:1521 \ |
| 35 | + -e ORACLE_PWD=oraclepwd \ |
| 36 | + container-registry.oracle.com/database/free:latest |
| 37 | + ``` |
| 38 | + |
| 39 | +4. Wait for the database to become ready: |
| 40 | + ```bash |
| 41 | + docker logs -f oracle-free |
| 42 | + ``` |
| 43 | + Wait until the logs show a readiness message such as `DATABASE IS READY TO USE!`, then press `Ctrl+C`. |
| 44 | + |
| 45 | +5. Create the application user: |
| 46 | + ```bash |
| 47 | + docker exec -it oracle-free sqlplus system/oraclepwd@FREEPDB1 |
| 48 | + ``` |
| 49 | + |
| 50 | + Then run: |
| 51 | + ```sql |
| 52 | + CREATE USER appuser IDENTIFIED BY apppassword; |
| 53 | + GRANT CONNECT TO appuser; |
| 54 | + GRANT CREATE SESSION TO appuser; |
| 55 | + ``` |
| 56 | + Exit the SQL editor. |
| 57 | + |
| 58 | +Default values for `ORACLE_USER`, `ORACLE_PASSWORD`, `ORACLE_DSN`, and `PORT` are already specified in the application, so no further configuration is necessary for this demo. |
| 59 | + |
| 60 | +## Run from Source with GraalPy |
| 61 | + |
| 62 | +1. Create a GraalPy virtual environment: |
| 63 | + ```bash |
| 64 | + ${GRAALPY:-graalpy} -m venv target/venv |
| 65 | + ``` |
| 66 | + |
| 67 | +2. Install the Python dependencies: |
| 68 | + ```bash |
| 69 | + target/venv/bin/graalpy -m pip install -r requirements.txt |
| 70 | + ``` |
| 71 | + |
| 72 | +3. Start the application: |
| 73 | + ```bash |
| 74 | + target/venv/bin/graalpy app.py |
| 75 | + ``` |
| 76 | + |
| 77 | +4. Test the endpoint from another terminal: |
| 78 | + ```bash |
| 79 | + curl http://localhost:8080/ |
| 80 | + ``` |
| 81 | + |
| 82 | + You should see this response: |
| 83 | + ```json |
| 84 | + {"message":"Hello from Oracle"} |
| 85 | + ``` |
| 86 | + |
| 87 | +## Build a Native Standalone Executable |
| 88 | + |
| 89 | + |
| 90 | +For building a Python standalone application, native executable, set `JAVA_HOME` and `PATH` to the required GraalVM version: |
| 91 | +```bash |
| 92 | +export JAVA_HOME="$HOME/.sdkman/candidates/java/<version>-graal" |
| 93 | +export PATH="$JAVA_HOME/bin:$PATH" |
| 94 | +``` |
| 95 | + |
| 96 | +The GraalPy command to build a standalone executable is: |
| 97 | +```bash |
| 98 | +graalpy -m standalone native \ |
| 99 | + --module app.py \ |
| 100 | + --output target/standalone-app \ |
| 101 | + --venv target/venv |
| 102 | +``` |
| 103 | + |
| 104 | +For convenience, the script `build-native.sh` is provided for you: it creates a virtual environment in `target/venv`, installs Flask and `python-oracledb` libraries, and builds a native standalone executable. |
| 105 | + |
| 106 | +1. Run it: |
| 107 | + ```bash |
| 108 | + ./build-native.sh |
| 109 | + ``` |
| 110 | + Building a native executable takes a few minutes. |
| 111 | + The file is created at `target/standalone-app`. |
| 112 | + |
| 113 | +2. Start this native application: |
| 114 | + ```bash |
| 115 | + ./target/standalone-app |
| 116 | + ``` |
| 117 | + |
| 118 | +3. Test it the same way as before: |
| 119 | + ```bash |
| 120 | + curl http://localhost:8080/ |
| 121 | + ``` |
| 122 | + |
| 123 | +## Summary |
| 124 | + |
| 125 | +This demo showcases a Flask application packaged as a GraalPy native executable, connecting to Oracle Database using the `python-oracledb` driver. |
0 commit comments