Session persistence and initialization

From: Oscar Maire-Richard (omair..idsa.es)
Date: Mon Mar 06 2006 - 04:47:07 EST

  • Next message: Mahmut Izci: "avoid caching on fetching by primary key"

    Hi all,
    I am having some problems with cayenne configuration and session
    persistence arised when I have moved login and password from the node
    file to the initialization java code. I will try to explain my environment.

    - I have two databases (data and users) inside a unique domain. The
    users data is optional for the application, it is just to manage the
    realm, but the authentication/authorization is delegated to Tomcat. My
    cayenne.xml is like this:

    <?xml version="1.0" encoding="utf-8"?>
    <domains project-version="1.1">
    <domain name="MyDomain">
        <property name="cayenne.DataRowStore.snapshot.size" value="1000"/>

        <map name="MyDataMap" location="MyDataMap.map.xml"/>
        <map name="MyUsersMap" location="MyUsersMap.map.xml"/>

        <node name="MyDataNode"
             datasource="MyDataNode.driver.xml"
             adapter="org.objectstyle.cayenne.dba.mysql.MySQLAdapter"
             factory="org.objectstyle.cayenne.conf.DriverDataSourceFactory">
                <map-ref name="MyDataMap"/>
         </node>
        <node name="MyUsersNode"
             datasource="MyUsersNode.driver.xml"
             adapter="org.objectstyle.cayenne.dba.mysql.MySQLAdapter"
             factory="org.objectstyle.cayenne.conf.DriverDataSourceFactory">
                <map-ref name="MyUsersMap"/>
         </node>
    </domain>
    </domains>

    - I initialize the configuration in a context listener extending
    WebApplicationListener
    When the logins/passwords were declared in
    "MyDataNode.driver.xml"/"MyUsersNode.driver.xml" I initialized with the
    following code in the listener:
      public static void init(ServletContext srvCtx) {
            BasicServletConfiguration.initializeConfiguration(srvCtx);
            DataDomain domain =
    Configuration.getSharedConfiguration().getDomain();
            DataNode node = domain.getNode("MyDataNode");
            JdbcAdapter adapter = (JdbcAdapter) node.getAdapter();
            adapter.setSupportsFkConstraints(true);
            node.setAdapter(adapter);
      }

    Every thing worked right.
    Note that I was not doing any thing whith "MyUsersNode", I do not know
    why, but it worked.

    - Now, I've removed logins/passwords from xml files, and changed the
    connections pool min from "1" to "0". Then my code to initialize is as
    follows:
        public static void init(ServletContext srvCtx) {
            BasicServletConfiguration.initializeConfiguration(srvCtx);
            DataDomain domain =
    Configuration.getSharedConfiguration().getDomain();
            DataNode node = domain.getNode("MyDataNode");
            try {
                int maxConn = ((PoolManager)
    node.getDataSource()).getMaxConnections();
                PoolManager dataSource = new PoolManager(
                        "com.mysql.jdbc.Driver",
                        "jdbc:mysql://localhost:3306/myDataDB",
                        1, maxConn,
                        "user",
                        "password");
                node.setDataSource(dataSource);
            } catch (SQLException e) {
                log.error("Cannot create DataSource.", e);
            }
            JdbcAdapter adapter = (JdbcAdapter) node.getAdapter();
            adapter.setSupportsFkConstraints(true);
            node.setAdapter(adapter);

            log.info("Users database connection initialization");
            DataNode usersNode = domain.getNode("MyUsersNode");
            try {
                int maxConn =
    ((PoolManager)usersNode.getDataSource()).getMaxConnections();
                PoolManager usersDataSource = new PoolManager(
                        "com.mysql.jdbc.Driver",
                        "jdbc:mysql://localhost:3306/myUsersDB",
                        1, maxConn,
                        "user",
                        "password");
                node.setDataSource(usersDataSource);
            } catch (SQLException e) {
                log.error("Cannot create DataSource.", e);
            }
            JdbcAdapter usersAdapter = (JdbcAdapter) usersNode.getAdapter();
            usersAdapter.setSupportsFkConstraints(true);
            usersNode.setAdapter(usersAdapter);
        }
    }

    Now, Cayenne look for all my references to DataObjects in MyUsersDB,
    thus I get the exception:

    java.sql.SQLException: General error message from server: "Table 'myusersdb.foo' doesn't exist"

    Here my first question:
    How should I configure and initialize both databases?

    - If I remove all staff related to MyUsersDB the application works
    right. But when I restart Tomcat (or just the application) the session
    that is recovered is not able to establish the database connection
    because the configuration is based on the xml file (without username and
    password) although the initialization was executed by listener. My
    second question is whether is it possible to achieve session persistence
    for data contexts?

    Thanks in advance,

    Oscar Maire-Richard
    SIDSA



    This archive was generated by hypermail 2.0.0 : Mon Mar 06 2006 - 04:46:01 EST