04-13-2011 06:59 AM
package pac1;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.Deployment;
import org.apache.commons.dbcp.BasicDataSource;
public class Prog3 {
public static void main(String[] args) throws Exception {
ProcessEngine processEngine = null;
BasicDataSource myDataSource = null;
try {
System.out.println("starting!");
int threadNumber = 15;
int maxConnectionNumber = 3;
myDataSource = new org.apache.commons.dbcp.BasicDataSource();
myDataSource.setDriverClassName("org.postgresql.Driver");
myDataSource.setUrl("jdbc:postgresql://127.0.0.1:54322/activiti");
myDataSource.setUsername("activiti");
myDataSource.setPassword("activiti");
myDataSource.setMaxActive(maxConnectionNumber);
myDataSource.setMinIdle(0);
System.out.println("max Active: " + myDataSource.getMaxActive());
DataSource ds = new HackedDataSource(myDataSource);
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
.createStandaloneProcessEngineConfiguration()
.setDataSource(ds);
processEngine = processEngineConfiguration.buildProcessEngine();
System.out.println("processEngine created OK!");
// Deploy the test process
RepositoryService repositoryService = processEngine
.getRepositoryService();
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("tescior3.bpmn20.xml").deploy();
System.out.println("process deployed OK!");
final RuntimeService runtimeService = processEngine
.getRuntimeService();
System.out.println("ok, got runtimeService");
// Start a number of process instances
List<Thread> myThreads = new ArrayList<Thread>();
for (int i = 0; i < threadNumber; i++) {
Thread thr = new Thread(new Runnable() {
@Override
public void run() {
runtimeService.startProcessInstanceByKey("test3");
}
});
thr.start();
myThreads.add(thr);
}
for (Thread t : myThreads) {
t.join();
}
System.out.println("ok, all threads joined");
} finally {
if (processEngine != null)
try {
System.out.println("about to close process engine…");
processEngine.close();
System.out.println("process engine closed");
} catch (Exception e) {
e.printStackTrace();
}
if (myDataSource != null)
try {
System.out.println("about to close data source…");
myDataSource.close();
System.out.println("data source closed");
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
04-13-2011 01:49 PM
04-14-2011 01:27 AM
Thread A Thread B
getConnection -> OK!
getConnection -> OK!
getConnection -> wait…
getConnection -> wait…
…now both threads are just stuck forever…
And that's a classic deadlock example.04-14-2011 03:15 AM
04-14-2011 04:12 AM
04-14-2011 04:20 AM
04-14-2011 04:54 AM
04-14-2011 05:07 AM
04-14-2011 05:09 AM
What if you change your pool with a datasource pooling solutiuon such as DBCP? The iBatis default connection pool is fairly simple implemented, last time I went through the code.Hi jbarrez,
myDataSource = new org.apache.commons.dbcp.BasicDataSource();
myDataSource.setDriverClassName("org.postgresql.Driver");
myDataSource.setUrl("jdbcostgresql://127.0.0.1:54322/activiti");
myDataSource.setUsername("activiti");
myDataSource.setPassword("activiti");
myDataSource.setMaxActive(maxConnectionNumber);
myDataSource.setMinIdle(0);
System.out.println("max Active: " + myDataSource.getMaxActive());
DataSource ds = new HackedDataSource(myDataSource);
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
.createStandaloneProcessEngineConfiguration()
.setDataSource(ds);
processEngine = processEngineConfiguration.buildProcessEngine();
System.out.println("processEngine created OK!");
public class HackedConnection implements Connection {
private Connection conn;
public HackedConnection(Connection conn) {
this.conn = conn;
}
public void close() throws SQLException {
String name = Thread.currentThread().getName();
System.out.println(" –Connection.close called for thread " + name
+ ".");
HackerDatabase.customerLeaves(name);
conn.close();
System.out.println(" –Connection.close finishes for thread " + name
+ ".");
}
..delegate methods…
public class HackedDataSource implements DataSource {
private DataSource dataSource;
private TreeSet<String> customers;
public HackedDataSource(DataSource dataSource) {
this.dataSource = dataSource;
this.customers = new TreeSet<String>();
}
public Connection getConnection() throws SQLException {
String name = Thread.currentThread().getName();
System.out.println(" –getConnection called for thread " + name + ".");
synchronized (customers) {
if (!"main".equals(name)) {
if (customers.contains(name))
System.out.println(" —-hey!");
customers.add(name);
}
}
HackerDatabase.customerAsking(name);
Connection conn = dataSource.getConnection();
HackerDatabase.customerServed(name);
System.out.println(" –getConnection finishes for thread " + name
+ ".");
return new HackedConnection(conn);
}
…delegate methods…
04-14-2011 05:19 AM
Tags
Find what you came for
We want to make your experience in Hyland Connect as valuable as possible, so we put together some helpful links.