Skip to content

Commit

Permalink
TxIsolationLevel enum
Browse files Browse the repository at this point in the history
- recently we fixed typo in one name, so I created this.
- the package; probably we should create some JDK adapter where we would
  collect JDK enhancements.

Signed-off-by: David Matějček <david.matejcek@omnifish.ee>
  • Loading branch information
dmatej committed Sep 18, 2022
1 parent bb2e6b1 commit 151c571
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 62 deletions.
10 changes: 6 additions & 4 deletions appserver/connectors/connectors-internal-api/osgi.bundle
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#
# Copyright (c) 2022 Eclipse Foundation and/or its affiliates. All rights reserved.
# Copyright (c) 2010, 2018 Oracle and/or its affiliates. All rights reserved.
#
# This program and the accompanying materials are made available under the
Expand All @@ -15,7 +16,8 @@
#

-exportcontents: com.sun.appserv.connectors.internal.api; \
org.glassfish.connectors.config; \
org.glassfish.connectors.config.validators; \
org.glassfish.api.jdbc.validation; \
com.sun.appserv.connectors.internal.spi; version=${project.osgi.version}
org.glassfish.connectors.config; \
org.glassfish.connectors.config.validators; \
org.glassfish.api.jdbc.objects; \
org.glassfish.api.jdbc.validation; \
com.sun.appserv.connectors.internal.spi; version=${project.osgi.version}
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
* Copyright (c) 2022 Eclipse Foundation and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* This Source Code may also be made available under the following Secondary
* Licenses when the conditions for such availability set forth in the
* Eclipse Public License v. 2.0 are satisfied: GNU General Public License,
* version 2 with the GNU Classpath Exception, which is available at
* https://www.gnu.org/software/classpath/license.html.
*
* SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
*/

package org.glassfish.api.jdbc.objects;

import java.sql.Connection;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.sql.Connection.TRANSACTION_READ_COMMITTED;
import static java.sql.Connection.TRANSACTION_READ_UNCOMMITTED;
import static java.sql.Connection.TRANSACTION_REPEATABLE_READ;
import static java.sql.Connection.TRANSACTION_SERIALIZABLE;

/**
* Enum for {@link Connection} TRANSACTION* constants referred in descriptors etc.
*
* @author David Matejcek
*/
public enum TxIsolationLevel {

/**
* A constant indicating that dirty reads, non-repeatable reads and phantom reads can occur.
* This level allows a row changed by one transaction to be read by another transaction before
* any changes in that row have been committed (a "dirty read").
* <p>
* If any of the changes are rolled back, the second transaction will have retrieved
* an invalid row.
*/
READ_UNCOMMITTED("read-uncommitted", TRANSACTION_READ_UNCOMMITTED),

/**
* A constant indicating that dirty reads are prevented;
* non-repeatable reads and phantom reads can occur.
* <p>
* This level only prohibits a transaction from reading a row with uncommitted changes in it.
*/
READ_COMMITTED("read-committed", TRANSACTION_READ_COMMITTED),

/**
* A constant indicating that dirty reads and non-repeatable reads are prevented;
* phantom reads can occur.
* <p>
* This level prohibits a transaction from reading a row with uncommitted changes in it, and it
* also prohibits the situation where one transaction reads a row, a second transaction alters
* the row, and the first transaction rereads the row, getting different values the second time
* (a "non-repeatable read").
*/
REPEATABLE_READ("repeatable-read", TRANSACTION_REPEATABLE_READ),

/**
* A constant indicating that dirty reads, non-repeatable reads and phantom reads are prevented.
* <p>
* This level includes the prohibitions in {@link #TRANSACTION_REPEATABLE_READ} and further
* prohibits the situation where one transaction reads all rows that satisfy
* a <code>WHERE</code> condition, a second transaction inserts a row that satisfies that
* <code>WHERE</code> condition, and the first transaction rereads for the same condition,
* retrieving the additional "phantom" row in the second read.
*/
SERIALIZABLE("serializable", TRANSACTION_SERIALIZABLE),
;

private String name;
private int id;

TxIsolationLevel(String name, int id) {
this.name = name;
this.id = id;
}

/**
* @return name used in descriptors etc.
*/
public String getName() {
return name;
}

/**
* @return one of {@link Connection} TRANSACTION* constants
*/
public int getId() {
return id;
}


/**
* @param id of the level
* @return {@link TxIsolationLevel}, never null
* @throws IllegalArgumentException if the id is not valid.
*/
public static TxIsolationLevel byId(int id) throws IllegalArgumentException {
return Stream.of(TxIsolationLevel.values()).filter(v -> v.getId() == id).findAny()
.orElseThrow(() -> new IllegalArgumentException("Invalid transaction isolation id; the transaction "
+ "isolation level can be any of the following: " + Stream.of(TxIsolationLevel.values())
.map(t -> Integer.toString(t.getId())).collect(Collectors.joining(", "))));
}


/**
* @param name of the level
* @return {@link TxIsolationLevel}, never null
* @throws IllegalArgumentException if the name is not valid.
*/
public static TxIsolationLevel byName(String name) throws IllegalArgumentException {
return Stream.of(TxIsolationLevel.values()).filter(v -> v.getName() == name).findAny()
.orElseThrow(() -> new IllegalArgumentException("Invalid transaction isolation; the transaction"
+ " isolation level can be empty or any of the following: " + Stream.of(TxIsolationLevel.values())
.map(TxIsolationLevel::getName).collect(Collectors.joining(", "))));
}


/**
* @return all defined names
*/
public static String[] getAllNames() {
return Stream.of(TxIsolationLevel.values()).map(TxIsolationLevel::getName).toArray(String[]::new);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022 Contributors to the Eclipse Foundation
* Copyright (c) 2021, 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2020 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@

import org.glassfish.api.jdbc.ConnectionValidation;
import org.glassfish.api.jdbc.SQLTraceListener;
import org.glassfish.api.jdbc.objects.TxIsolationLevel;
import org.glassfish.external.probe.provider.PluginPoint;
import org.glassfish.external.probe.provider.StatsProviderManager;
import org.glassfish.resourcebase.resources.api.PoolInfo;
Expand Down Expand Up @@ -108,7 +109,7 @@ public abstract class ManagedConnectionFactoryImpl
protected LazyEnlistableConnectionManager connectionManager;
protected boolean isLazyConnectionManager;

private JdbcObjectsFactory jdbcObjectsFactory = JdbcObjectsFactory.getInstance();
private final JdbcObjectsFactory jdbcObjectsFactory = JdbcObjectsFactory.getInstance();
private int statementCacheSize;
private String statementCacheType;
private long statementLeakTimeout;
Expand Down Expand Up @@ -284,7 +285,7 @@ public ManagedConnection matchManagedConnections(Set connectionSet, Subject subj
@Override
public Set getInvalidConnections(Set connectionSet) throws ResourceException {
Iterator iter = connectionSet.iterator();
Set<ManagedConnectionImpl> invalidConnections = new HashSet<ManagedConnectionImpl>();
Set<ManagedConnectionImpl> invalidConnections = new HashSet<>();
while (iter.hasNext()) {
ManagedConnectionImpl managedConnectionImpl = (ManagedConnectionImpl) iter.next();
try {
Expand Down Expand Up @@ -574,8 +575,7 @@ private void detectSqlTraceListeners() {
_logger.log(SEVERE, "jdbc.sql_trace_listener_cnfe", sqlTraceListener);
}
Class intf[] = listenerClass.getInterfaces();
for (int i = 0; i < intf.length; i++) {
Class interfaceName = intf[i];
for (Class interfaceName : intf) {
if (interfaceName.getName().equals("org.glassfish.api.jdbc.SQLTraceListener")) {

try {
Expand Down Expand Up @@ -617,31 +617,15 @@ private void detectSqlTraceListeners() {
* the string specifying the isolation.
*/
private int getTransactionIsolationInt(String tranIsolation) throws ResourceException {
if (tranIsolation.equalsIgnoreCase("read-uncommitted")) {
return Connection.TRANSACTION_READ_UNCOMMITTED;
}

if (tranIsolation.equalsIgnoreCase("read-committed")) {
return Connection.TRANSACTION_READ_COMMITTED;
}

if (tranIsolation.equalsIgnoreCase("repeatable-read")) {
return Connection.TRANSACTION_REPEATABLE_READ;
}

if (tranIsolation.equalsIgnoreCase("serializable")) {
return Connection.TRANSACTION_SERIALIZABLE;
try {
return TxIsolationLevel.byName(tranIsolation).getId();
} catch (IllegalArgumentException e) {
throw new ResourceException(e.getMessage(), e);
}

throw new ResourceException(
"Invalid transaction isolation; the transaction " +
"isolation level can be empty or any of the following: " +
"read-uncommitted, read-committed, repeatable-read, serializable");
}

/**
* Common operation performed by all the child MCFs before returning a created
* mc
* Common operation performed by all the child MCFs before returning a created mc
*/
protected void validateAndSetIsolation(ManagedConnectionImpl managedConnectionImpl) throws ResourceException {
try {
Expand Down Expand Up @@ -1352,8 +1336,9 @@ protected void computeStatementWrappingStatus() {

boolean poolProperty = false;
String statementWrappingString = getStatementWrapping();
if (statementWrappingString != null)
if (statementWrappingString != null) {
poolProperty = Boolean.valueOf(statementWrappingString);
}

if (wrapStatement == JVM_OPTION_STATEMENT_WRAPPING_ON
|| (wrapStatement == JVM_OPTION_STATEMENT_WRAPPING_NOT_SET && poolProperty)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
Expand All @@ -16,18 +17,20 @@

package org.glassfish.jdbc.deployer;

import static com.sun.appserv.connectors.internal.api.ConnectorConstants.JAVAX_SQL_CONNECTION_POOL_DATASOURCE;
import static com.sun.appserv.connectors.internal.api.ConnectorConstants.JAVAX_SQL_DATASOURCE;
import static com.sun.appserv.connectors.internal.api.ConnectorConstants.JAVAX_SQL_XA_DATASOURCE;
import static com.sun.appserv.connectors.internal.api.ConnectorConstants.JAVA_SQL_DRIVER;
import static com.sun.appserv.connectors.internal.api.ConnectorsUtil.deriveResourceName;
import static com.sun.appserv.connectors.internal.api.ConnectorsUtil.getTransactionIsolationInt;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.FINEST;
import static java.util.logging.Level.WARNING;
import static org.glassfish.deployment.common.JavaEEResourceType.DSDPOOL;
import static org.glassfish.resourcebase.resources.api.ResourceConstants.JAVA_APP_SCOPE_PREFIX;
import static org.glassfish.resourcebase.resources.api.ResourceConstants.JAVA_GLOBAL_SCOPE_PREFIX;
import com.sun.enterprise.config.serverbeans.Application;
import com.sun.enterprise.config.serverbeans.Resource;
import com.sun.enterprise.config.serverbeans.Resources;
import com.sun.enterprise.deployment.BundleDescriptor;
import com.sun.enterprise.deployment.DataSourceDefinitionDescriptor;
import com.sun.enterprise.deployment.EjbBundleDescriptor;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.EjbInterceptor;
import com.sun.enterprise.deployment.JndiNameEnvironment;
import com.sun.enterprise.deployment.ManagedBeanDescriptor;
import com.sun.logging.LogDomains;

import jakarta.inject.Inject;
import jakarta.inject.Provider;

import java.beans.PropertyVetoException;
import java.sql.Driver;
Expand All @@ -44,6 +47,7 @@
import javax.sql.DataSource;
import javax.sql.XADataSource;

import org.glassfish.api.jdbc.objects.TxIsolationLevel;
import org.glassfish.deployment.common.Descriptor;
import org.glassfish.deployment.common.JavaEEResourceType;
import org.glassfish.deployment.common.RootDeploymentDescriptor;
Expand All @@ -61,20 +65,17 @@
import org.jvnet.hk2.config.TransactionFailure;
import org.jvnet.hk2.config.types.Property;

import com.sun.enterprise.config.serverbeans.Application;
import com.sun.enterprise.config.serverbeans.Resource;
import com.sun.enterprise.config.serverbeans.Resources;
import com.sun.enterprise.deployment.BundleDescriptor;
import com.sun.enterprise.deployment.DataSourceDefinitionDescriptor;
import com.sun.enterprise.deployment.EjbBundleDescriptor;
import com.sun.enterprise.deployment.EjbDescriptor;
import com.sun.enterprise.deployment.EjbInterceptor;
import com.sun.enterprise.deployment.JndiNameEnvironment;
import com.sun.enterprise.deployment.ManagedBeanDescriptor;
import com.sun.logging.LogDomains;

import jakarta.inject.Inject;
import jakarta.inject.Provider;
import static com.sun.appserv.connectors.internal.api.ConnectorConstants.JAVAX_SQL_CONNECTION_POOL_DATASOURCE;
import static com.sun.appserv.connectors.internal.api.ConnectorConstants.JAVAX_SQL_DATASOURCE;
import static com.sun.appserv.connectors.internal.api.ConnectorConstants.JAVAX_SQL_XA_DATASOURCE;
import static com.sun.appserv.connectors.internal.api.ConnectorConstants.JAVA_SQL_DRIVER;
import static com.sun.appserv.connectors.internal.api.ConnectorsUtil.deriveResourceName;
import static java.util.logging.Level.FINE;
import static java.util.logging.Level.FINEST;
import static java.util.logging.Level.WARNING;
import static org.glassfish.deployment.common.JavaEEResourceType.DSDPOOL;
import static org.glassfish.resourcebase.resources.api.ResourceConstants.JAVA_APP_SCOPE_PREFIX;
import static org.glassfish.resourcebase.resources.api.ResourceConstants.JAVA_GLOBAL_SCOPE_PREFIX;

/**
* @author Jagadish Ramu
Expand Down Expand Up @@ -542,8 +543,8 @@ public Property removeProperty(Property prprt) {

class MyJdbcConnectionPool extends FakeConfigBean implements JdbcConnectionPool {

private DataSourceDefinitionDescriptor dataSourceDefinitionDescriptor;
private String name;
private final DataSourceDefinitionDescriptor dataSourceDefinitionDescriptor;
private final String name;

public MyJdbcConnectionPool(DataSourceDefinitionDescriptor desc, String name) {
this.dataSourceDefinitionDescriptor = desc;
Expand Down Expand Up @@ -681,8 +682,7 @@ public String getTransactionIsolationLevel() {
if (dataSourceDefinitionDescriptor.getIsolationLevel() == -1) {
return null;
}

return getTransactionIsolationInt(dataSourceDefinitionDescriptor.getIsolationLevel());
return TxIsolationLevel.byId(dataSourceDefinitionDescriptor.getIsolationLevel()).getName();

}

Expand Down Expand Up @@ -924,7 +924,7 @@ public void setDescription(String value) throws PropertyVetoException {
@Override
public List<Property> getProperty() {
Properties descriptorProperties = dataSourceDefinitionDescriptor.getProperties();
List<Property> dataSourceProperties = new ArrayList<Property>();
List<Property> dataSourceProperties = new ArrayList<>();

for (Map.Entry<Object, Object> entry : descriptorProperties.entrySet()) {
String key = (String) entry.getKey();
Expand Down

0 comments on commit 151c571

Please sign in to comment.