You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@cayenne.apache.org by Mathias Clerc <tl...@gmail.com> on 2011/12/09 09:04:17 UTC

Deadlock with Derby and CreateIfNoSchemaStrategy

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 ?

Re: Deadlock with Derby and CreateIfNoSchemaStrategy

Posted by Mathias Clerc <tl...@gmail.com>.
Thanks Arnaud.

I won't have time to try it before early January I am afraid but I
definitely will.


2011/12/20 Arnaud Garcia <ar...@imagemed-87.com>:
> 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 <tl...@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 ?
>>

Re: Deadlock with Derby and CreateIfNoSchemaStrategy

Posted by Arnaud Garcia <ar...@imagemed-87.com>.
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 <tl...@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 ?
>

Re: Deadlock with Derby and CreateIfNoSchemaStrategy

Posted by Mathias Clerc <tl...@gmail.com>.
2011/12/10 Aristedes Maniatis <ar...@maniatis.org>:
> On Sat Dec 10 16:39:50 2011, Andrus Adamchik wrote:
>>
>> Could be a bug (in Derby?). Probably worth opening a bug report at
>> https://issues.apache.org/jira/browse/CAY ... Also would be helpful if you
>> could take a thread dump when the app is frozen to see what the app is doing
>> at this moment. IIRC we had people using CreateIfNoSchemaStrategy with
>> Derby, so there may be a special case causing this condition.
>>
>> Andrus
>>
>> On Dec 8, 2011, at 10:04 PM, Mathias Clerc wrote:
>>
>>> 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 ?
>>>
>>
>
> I also recommend searching the Derby Jira database for "deadlock". There are
> a number of deadlock issues in recent Derby versions. Although I've never
> seen it in the situation the original poster reported, I've seen other
> similar deadlocks when trying to alter the schema of a Derby database.
>
> --
> -------------------------->
> Aristedes Maniatis
> GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A

Thank you for the answers, I will go through the derby open bugs later
and open issues where needed.

Re: Deadlock with Derby and CreateIfNoSchemaStrategy

Posted by Aristedes Maniatis <ar...@maniatis.org>.
On Sat Dec 10 16:39:50 2011, Andrus Adamchik wrote:
> Could be a bug (in Derby?). Probably worth opening a bug report at https://issues.apache.org/jira/browse/CAY ... Also would be helpful if you could take a thread dump when the app is frozen to see what the app is doing at this moment. IIRC we had people using CreateIfNoSchemaStrategy with Derby, so there may be a special case causing this condition.
>
> Andrus
>
> On Dec 8, 2011, at 10:04 PM, Mathias Clerc wrote:
>
>> 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 ?
>>
>

I also recommend searching the Derby Jira database for "deadlock". 
There are a number of deadlock issues in recent Derby versions. 
Although I've never seen it in the situation the original poster 
reported, I've seen other similar deadlocks when trying to alter the 
schema of a Derby database.

-- 
-------------------------->
Aristedes Maniatis
GPG fingerprint CBFB 84B4 738D 4E87 5E5C  5EFA EF6A 7D2E 3E49 102A

Re: Deadlock with Derby and CreateIfNoSchemaStrategy

Posted by Andrus Adamchik <an...@objectstyle.org>.
Could be a bug (in Derby?). Probably worth opening a bug report at https://issues.apache.org/jira/browse/CAY ... Also would be helpful if you could take a thread dump when the app is frozen to see what the app is doing at this moment. IIRC we had people using CreateIfNoSchemaStrategy with Derby, so there may be a special case causing this condition.

Andrus

On Dec 8, 2011, at 10:04 PM, Mathias Clerc wrote:

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