Skip to content

Data Source & Resource Bundle

Getting a Connection from the Data Source

For some reason, you might need to get a connection from the data source directly, for example, to perform batch processing or custom database operations. Here's how you can do that:

from webgeaz.sufia.core import Application

class BatchProcessing(object):
    def execute(self, ctx):
        data_source = Application.getDs("default")
        try:
            # Get a connection from the data source
            conn = data_source.getConnection()
            # do something with the connection
        finally:
            # Ensure the connection is closed to avoid resource leaks
            if conn:
                conn.close()

Note

In Java, a DataSource represents a facility for storing data, most commonly a database. It is a key component in Java Database Connectivity (JDBC) and serves as a factory for creating connections to a specific data source.

Use this approach with caution, as it bypasses the transaction management provided by SØAD. Always ensure connections are closed properly to avoid resource leaks.

Using properties from the Resource Bundle (sufia.properties)

SØAD allows you to access properties defined in the sufia.properties file, which can be useful for configuration settings, API keys, or other constants. Here's how to retrieve a property:

from webgeaz.sufia.core import Application

class Home(object):
    def __init__(self):
        # Get a property from the resource bundle
        self.secret_key = Application.getProperty("my.secret.key")

    def view(self, ctx):
        response = call_some_service(self.secret_key)
        ...

Note

Java Properties files, typically with a .properties extension, are used in Java applications primarily for managing configuration data and localization strings.

Properties files are widely used to store application-specific configurations. This can include:

  • Database connection details (URLs, usernames, passwords).
  • Environment settings (e.g., development, testing, production).
  • API keys or service endpoints.
  • Application-specific parameters and flags.