cayenne-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arnaud Garcia <arn...@imagemed-87.com>
Subject Re: Deadlock with Derby and CreateIfNoSchemaStrategy
Date Tue, 20 Dec 2011 12:44:52 GMT
Hi Mathias,

It works for me with embedded derby, I don't remember why but I have
created my own CreateIfNoSchemaStrategy...
well here the code, hope it helps and if it works for you take it as my
christmas gift ;-)

Arnaud

----
      try {

            DataSource dataSource = new
PoolManager("org.apache.derby.jdbc.EmbeddedDriver", "jdbc:derby:" +
dBDir.getPath() + "/vpdb;create=true", 1, 5, "", "");
            Configuration config = Configuration.getSharedConfiguration();
            DataDomain domain = config.getDomain();
            DataNode node = domain.getNode("VPNode");
            node.setDataSource(dataSource);
            node.setSchemaUpdateStrategy(new SXCreateIfNoSchemaStrategy());
        } catch ( SQLException ex ) {

        }
--------
public class SXCreateIfNoSchemaStrategy extends CreateIfNoSchemaStrategy {

    @Override
    public void processSchemaUpdate(DataNode dataNode) throws SQLException {
        Map<String, Boolean> nameTables = getNameTablesInDB(dataNode);
        Collection<DbEntity> entities =
dataNode.getEntityResolver().getDbEntities();
        boolean generate = true;
        Iterator<DbEntity> it = entities.iterator();
        while (it.hasNext()) {
            if (nameTables.get(it.next().getName().toUpperCase()) != null) {
                generate = false;
                break;
            }
        }

        if (generate) {
            LogUtilities.getLogger(getClass()).info("No schema detected,
will create mapped tables");
            generate(dataNode);
        } else {
            LogUtilities.getLogger(getClass()).info("Full or partial schema
detected, skipping tables creation");
        }
    }

    private void generate(DataNode dataNode) {
        Collection<DataMap> map = dataNode.getDataMaps();
        Iterator<DataMap> iterator = map.iterator();
        while (iterator.hasNext()) {
            DbGenerator gen = new DbGenerator(dataNode.getAdapter(),
iterator.next());
            gen.setShouldCreateTables(true);
            gen.setShouldDropTables(false);
            gen.setShouldCreateFKConstraints(true);
            gen.setShouldCreatePKSupport(true);
            gen.setShouldDropPKSupport(false);
            try {
                gen.runGenerator(dataNode.getDataSource());
            } catch (Exception e) {
                throw new CayenneRuntimeException(e);
            }
        }
    }
}

-------
2011/12/9 Mathias Clerc <tlarhices@gmail.com>

> I am using an embedded Derby DB together with Cayenne
> CreateIfNoSchemaStrategy feature.
>
> If I start with an empty DB, on the first query the DB is created but
> at the moment of actually running the query, Derby freezes waiting for
> a lock.
> If I use JDBC to manually create the table before the first connection
> (manually injecting a copy/paste of the debug output of cayenne), then
> the query works perfectly.
>
> Any idea ?
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message