Skip to content

Commit

Permalink
LDEV-3980
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed May 13, 2022
1 parent 36a62cb commit 1668f6d
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 42 deletions.
97 changes: 57 additions & 40 deletions core/src/main/java/lucee/runtime/db/DatasourceManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ public final class DatasourceManagerImpl implements DataSourceManager {
private int isolation = Connection.TRANSACTION_NONE;
private Map<DataSource, DatasourceConnection> transConnsReg = new HashMap<DataSource, DatasourceConnection>();
private Map<DataSource, ORMDatasourceConnection> transConnsORM = new HashMap<DataSource, ORMDatasourceConnection>();
private static final ConcurrentHashMap<String, String> tokens = new ConcurrentHashMap<String, String>();
private boolean inside;

private Map<String, Savepoint> savepoints = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -88,13 +89,18 @@ public DatasourceConnection getConnection(PageContext pc, DataSource ds, String

// first time that datasource is used within this transaction
if (existingDC == null) {
DatasourceConnection newDC = config.getDatasourceConnectionPool().getDatasourceConnection(config, ds, user, pass);
if (!autoCommit) {
newDC.setAutoCommit(false);
if (isolation != Connection.TRANSACTION_NONE) DBUtil.setTransactionIsolationEL(newDC.getConnection(), isolation);
synchronized (getToken(ds.id())) {
existingDC = (DatasourceConnectionPro) transConnsReg.get(ds);
if (existingDC == null) {
DatasourceConnection newDC = config.getDatasourceConnectionPool().getDatasourceConnection(config, ds, user, pass);
if (!autoCommit) {
newDC.setAutoCommit(false);
if (isolation != Connection.TRANSACTION_NONE) DBUtil.setTransactionIsolationEL(newDC.getConnection(), isolation);
}
transConnsReg.put(ds, newDC);
return newDC;
}
}
transConnsReg.put(ds, newDC);
return newDC;
}

// we have already the same datasource but with different credentials
Expand Down Expand Up @@ -219,9 +225,9 @@ public void rollback(String savePointName) throws DatabaseException {
Pair<DatasourceConnection, Exception> pair = null;
boolean hasSavePointMatch = false;

// Reg
// ORM
{
Iterator<DatasourceConnection> it = this.transConnsReg.values().iterator();
Iterator<ORMDatasourceConnection> it = this.transConnsORM.values().iterator();
while (it.hasNext()) {
dc = it.next();
try {
Expand All @@ -242,9 +248,9 @@ public void rollback(String savePointName) throws DatabaseException {
}
}
}
// ORM
// Reg
{
Iterator<ORMDatasourceConnection> it = this.transConnsORM.values().iterator();
Iterator<DatasourceConnection> it = this.transConnsReg.values().iterator();
while (it.hasNext()) {
dc = it.next();
try {
Expand Down Expand Up @@ -280,15 +286,14 @@ public void savepoint(String savePointName) throws DatabaseException {

DatasourceConnection dc;
Pair<DatasourceConnection, Exception> pair = null;
// Reg
// ORM
{
Iterator<DatasourceConnection> it = this.transConnsReg.values().iterator();
Iterator<ORMDatasourceConnection> it = this.transConnsORM.values().iterator();
while (it.hasNext()) {
dc = it.next();
try {

if (savePointName == null) dc.getConnection().setSavepoint();
else savepoints.put(toKey(dc.getDatasource(), savePointName), dc.getConnection().setSavepoint(savePointName));
else dc.getConnection().setSavepoint(savePointName);
}
catch (Exception e) {
// we only keep the first exception
Expand All @@ -298,14 +303,15 @@ public void savepoint(String savePointName) throws DatabaseException {
}
}
}
// ORM
// Reg
{
Iterator<ORMDatasourceConnection> it = this.transConnsORM.values().iterator();
Iterator<DatasourceConnection> it = this.transConnsReg.values().iterator();
while (it.hasNext()) {
dc = it.next();
try {

if (savePointName == null) dc.getConnection().setSavepoint();
else dc.getConnection().setSavepoint(savePointName);
else savepoints.put(toKey(dc.getDatasource(), savePointName), dc.getConnection().setSavepoint(savePointName));
}
catch (Exception e) {
// we only keep the first exception
Expand All @@ -328,9 +334,9 @@ public void commit() throws DatabaseException {

Pair<DatasourceConnection, Exception> pair = null;
DatasourceConnection dc;
// Reg
// ORM
{
Iterator<DatasourceConnection> it = this.transConnsReg.values().iterator();
Iterator<ORMDatasourceConnection> it = this.transConnsORM.values().iterator();
while (it.hasNext()) {
dc = it.next();
try {
Expand All @@ -344,9 +350,9 @@ public void commit() throws DatabaseException {
}
}
}
// ORM
// Reg
{
Iterator<ORMDatasourceConnection> it = this.transConnsORM.values().iterator();
Iterator<DatasourceConnection> it = this.transConnsReg.values().iterator();
while (it.hasNext()) {
dc = it.next();
try {
Expand Down Expand Up @@ -387,23 +393,19 @@ public void end() { // FUTURE add DatabaseException
public void end(boolean onlyORM) {
autoCommit = true;
Pair<DatasourceConnection, Exception> pair = null;
// Reg

savepoints.clear();

if (transConnsReg.size() > 0) {
// ORM
if (transConnsORM.size() > 0) {
Map<DataSource, DatasourceConnection> tmp = null;
if (onlyORM) tmp = new HashMap<DataSource, DatasourceConnection>();
Iterator<Entry<DataSource, DatasourceConnection>> it = this.transConnsReg.entrySet().iterator();
Iterator<Entry<DataSource, ORMDatasourceConnection>> it = this.transConnsORM.entrySet().iterator();
DatasourceConnection dc;
Entry<DataSource, DatasourceConnection> entry;
Entry<DataSource, ORMDatasourceConnection> entry;
while (it.hasNext()) {
entry = it.next();
dc = entry.getValue();
try {
if (onlyORM && !(dc.getConnection() instanceof ORMConnection)) {
tmp.put(entry.getKey(), entry.getValue());
continue;
}
dc.setAutoCommit(true);
DBUtil.setTransactionIsolationEL(dc.getConnection(), ((DatasourceConnectionPro) dc).getDefaultTransactionIsolation());

Expand All @@ -415,25 +417,30 @@ public void end(boolean onlyORM) {
}
continue;
}
releaseConnection(null, dc, true);
}
transConnsReg.clear();
if (onlyORM) transConnsReg = tmp;
transConnsORM.clear();
}

// ORM
if (transConnsORM.size() > 0) {
// Reg
if (transConnsReg.size() > 0) {
Map<DataSource, DatasourceConnection> tmp = null;
Iterator<Entry<DataSource, ORMDatasourceConnection>> it = this.transConnsORM.entrySet().iterator();
if (onlyORM) tmp = new HashMap<DataSource, DatasourceConnection>();
Iterator<Entry<DataSource, DatasourceConnection>> it = this.transConnsReg.entrySet().iterator();
DatasourceConnection dc;
Entry<DataSource, ORMDatasourceConnection> entry;
Entry<DataSource, DatasourceConnection> entry;
while (it.hasNext()) {
entry = it.next();
dc = entry.getValue();
try {
dc.setAutoCommit(true);
DBUtil.setTransactionIsolationEL(dc.getConnection(), ((DatasourceConnectionPro) dc).getDefaultTransactionIsolation());
if (onlyORM && !(dc.getConnection() instanceof ORMConnection)) {
tmp.put(entry.getKey(), entry.getValue());
continue;
}

if (!dc.getAutoCommit()) {
dc.setAutoCommit(true);
DBUtil.setTransactionIsolationEL(dc.getConnection(), ((DatasourceConnectionPro) dc).getDefaultTransactionIsolation());
}
}
catch (Exception e) {
// we only keep the first exception
Expand All @@ -442,9 +449,12 @@ public void end(boolean onlyORM) {
}
continue;
}
releaseConnection(null, dc, true);
}
transConnsORM.clear();
transConnsReg.clear();
if (onlyORM) transConnsReg = tmp;
}

this.isolation = Connection.TRANSACTION_NONE;

if (pair != null) {
Expand Down Expand Up @@ -478,4 +488,11 @@ private int _size() {
return transConnsORM.size() + transConnsReg.size();
}

public static String getToken(String key) {
String lock = tokens.putIfAbsent(key, key);
if (lock == null) {
lock = key;
}
return lock;
}
}
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project default="core" basedir="." name="Lucee" xmlns:artifact="antlib:org.apache.maven.artifact.ant">

<property name="version" value="5.3.9.140-SNAPSHOT"/>
<property name="version" value="5.3.9.141-SNAPSHOT"/>

<path id="maven-ant-tasks.classpath" path="../ant/lib/maven-ant-tasks-2.1.3.jar" />
<typedef resource="org/apache/maven/artifact/ant/antlib.xml"
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>5.3.9.140-SNAPSHOT</version>
<version>5.3.9.141-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down

0 comments on commit 1668f6d

Please sign in to comment.