Search code examples
javajdbcormapache-cayenne

Obtaining DataSource from Cayenne DataConext


Currently I am using Cayenne as my ORM. I need to get DataSource for initializing Velocity Engine in my code. I can manually create the datasource but I don't want to do it and want to use the existing datasource from Cayenne.


Solution

  • In Cayenne 3.1 it is rather trivial:

    ServerRuntime runtime = .. // this exists in every app
    DataSource ds = runtime.getDataSource("MyDataNode");
    

    In the earlier versions it is only marginally harder:

    DataDomain dd = context.getParentDataDomain();
    DataSource ds = dd.getDataNode("MyDataNode").getDataSource();
    

    The last approach works on 3.1 too BTW.