Re: Session persistence and initialization

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Mon Mar 06 2006 - 13:51:59 EST

  • Next message: McDaniel, Joe R.: "Getting object from cache?"

    Hi,

    Can't comment on your specific initialization method, but if your
    goal is to get rid of passwords in Cayenne XML files I suggest to
    configure Cayenne to use DataSource obtained via JNDI. In
    CayenneModeler change DataSource Factory for the DataNodes to
    JNDIDataSourceFactory (available from the dropdown). The check the
    Tomcat docs for your specific version on how to configure a JNDI
    DataSource on Tomcat end (IIRC relevant part of server.xml
    configuration format has changed between Tomcat 5.0 and 5.5). Here is
    a link to the older Tomcat config:

    http://objectstyle.org/cayenne/userguide/deploy/jndi.html

    > 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?

    Deserialized DataContext attaches itself to the existing Cayenne
    stack, regardless of how such stack is configured. So it is totally
    possible to restore the sessions after the server restart. There can
    be some timing issues (things happening in the wrong order). But
    before we dig into that, I suggest trying JNDI configuration above
    and the latest version of Cayenne (as there were deserialization bugs
    in the past that are fixed in latest 1.2 milestones).

    Andrus

    On Mar 6, 2006, at 12:47 PM, Oscar Maire-Richard wrote:
    > 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 - 13:52:01 EST