Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions jdo/identity/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xs
<artifactId>datanucleus-api-jdo</artifactId>
<version>[${api.jdo.min.version}, ${api.jdo.max.version})</version>
</dependency>
<dependency>
<groupId>com.github.zabetak</groupId>
<artifactId>jdbc-faulty</artifactId>
<version>2.0</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/**********************************************************************
Copyright (c) 2004 Andy Jefferson and others. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors :
2025 Stamatis Zampetakis - Test reproducing a connection leak when failures occur during value generation
***********************************************************************/
package org.datanucleus.tests;

import com.github.zabetak.jdbc.faulty.ExceptionFault;
import com.github.zabetak.jdbc.faulty.FaultyJDBCDriver;
import org.datanucleus.samples.valuegeneration.TableGeneratorItem;

import javax.jdo.PersistenceManager;
import javax.jdo.Transaction;
import java.util.Properties;

/**
* Test value generators in the presence of random failures when calling {@link java.sql.Connection} methods.
*/
public class ValueGeneratorRandomFailureTest extends JDOPersistenceTestCase
{
private static Properties userProperties() {
Properties factoryProperties = TestHelper.getFactoryProperties(1, null);
String url = factoryProperties.getProperty("datanucleus.ConnectionURL");
// Add the faulty proxy in front of the regular JDBC driver
String faultyURL = url.replace("jdbc", "jdbc:faulty");
Properties userProps = new Properties();
userProps.put("datanucleus.connectionURL", faultyURL);
return userProps;
}

public ValueGeneratorRandomFailureTest(String name) {
super(name, userProperties());
}

/**
* In normal circumstances, the test should finish normally even though there are some occasional errors during
* commit. Due to the connection leak, the test currently fails (most often with lock timeout exceptions).
*/
public void testTableGeneratorWithFailureOnCommitFinishesWithoutErrors()
{
try {
for (int i = 0; i < 100; i++) {
PersistenceManager pm = pmf.getPersistenceManager();
Transaction tx = pm.currentTransaction();
try {
tx.begin();
// Generate a random exception on 10% of the calls to Connection#commit
FaultyJDBCDriver.addFault("ex-fault-1", new ExceptionFault(0.1, "commit"));
pm.makePersistent(new TableGeneratorItem("Item_" + i));
FaultyJDBCDriver.clearFaults();
tx.commit();
}
catch(Exception e)
{
// If it is the artificially generated faulty exception ignore it and continue
// otherwise stop and propagate it
if (!isFaultyException(e)) {
throw e;
}
}
finally
{
if (tx.isActive()) {
tx.rollback();
}
pm.close();
}
}
}
finally
{
// Remove all registered faults
FaultyJDBCDriver.clearFaults();
// Clean out any created data
clean(TableGeneratorItem.class);
}
}

private static boolean isFaultyException(Throwable e) {
if (e == null) {
return false;
}
if (e.getMessage() != null && "Random exception generated by jdbc-faulty".equals(e.getMessage())) {
return true;
}
return isFaultyException(e.getCause());
}

}