Author: tolusha
Date: 2011-05-23 02:57:15 -0400 (Mon, 23 May 2011)
New Revision: 4401
Added:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/DataSourceProvider.java
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/DataSourceProviderImpl.java
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/ManagedConnection.java
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/ManagedDataSource.java
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/AbstractTransactionService.java
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/jdbc/
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/jdbc/impl/
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/jdbc/impl/TestDataSourceProviderImpl.java
Modified:
kernel/trunk/exo.kernel.component.common/pom.xml
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/ExoResource.java
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionException.java
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionResource.java
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionService.java
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jboss/JBossTransactionService.java
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jotm/ResourceEntry.java
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jotm/TransactionServiceJotmImpl.java
kernel/trunk/exo.kernel.component.common/src/main/resources/conf/portal/configuration.xml
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/TransactionTest.java
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/XAResourceTestImpl.java
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/jcr/TestXASessionIntegration.java
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/jcr/XASession.java
kernel/trunk/exo.kernel.component.common/src/test/resources/conf/standalone/test-configuration.xml
Log:
EXOJCR-480: Managed transactions support
Modified: kernel/trunk/exo.kernel.component.common/pom.xml
===================================================================
--- kernel/trunk/exo.kernel.component.common/pom.xml 2011-05-20 14:38:19 UTC (rev 4400)
+++ kernel/trunk/exo.kernel.component.common/pom.xml 2011-05-23 06:57:15 UTC (rev 4401)
@@ -125,10 +125,7 @@
<name>jgroups.stack</name>
<value>udp</value>
</property>
- </systemProperties>
- <excludes>
- <exclude>**/TransactionTest.java</exclude>
- </excludes>
+ </systemProperties>
</configuration>
</plugin>
<plugin>
Added:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/DataSourceProvider.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/DataSourceProvider.java
(rev 0)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/DataSourceProvider.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -0,0 +1,52 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.jdbc;
+
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+
+/**
+ * This provider is used to get a {@link DataSource} in an uniform manner.
+ * It allows to wrap the {@link DataSource} in case it is defined as managed
+ *
+ * @author <a href="mailto:nfilotto@exoplatform.com">Nicolas
Filotto</a>
+ * @version $Id$
+ *
+ */
+public interface DataSourceProvider
+{
+ /**
+ * Try to get the data source from a lookup, if it can't a {@link
NamingException}
+ * will be thrown
+ * @param dataSourceName the name of the data source to lookup
+ * @return the {@link DataSource} found thanks to the lookup. The original
+ * object could be wrap to another {@link DataSource} in order to support
+ * managed data source.
+ * @throws NamingException if the data source could not be found
+ */
+ DataSource getDataSource(String dataSourceName) throws NamingException;
+
+ /**
+ * Indicates whether or not the given data source is managed
+ * @param dataSourceName the data source to check
+ * @return <code>true</code> if the data source is managed,
+ * <code>false</code> otherwise
+ */
+ boolean isManaged(String dataSourceName);
+}
Added:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/DataSourceProviderImpl.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/DataSourceProviderImpl.java
(rev 0)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/DataSourceProviderImpl.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -0,0 +1,160 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.jdbc.impl;
+
+import org.exoplatform.container.xml.InitParams;
+import org.exoplatform.container.xml.ValueParam;
+import org.exoplatform.container.xml.ValuesParam;
+import org.exoplatform.services.jdbc.DataSourceProvider;
+import org.exoplatform.services.transaction.TransactionService;
+
+import java.util.HashSet;
+import java.util.Set;
+import java.util.StringTokenizer;
+
+import javax.naming.InitialContext;
+import javax.naming.NamingException;
+import javax.sql.DataSource;
+import javax.transaction.TransactionManager;
+
+/**
+ * The default implementation of {@link DataSourceProvider}. It allows you
+ * to define a data source as managed thanks to the configuration of this
+ * component. When the data source is declared as managed, the {@link DataSource}
+ * object will be wrap into a {@link ManagedDataSource}.
+ *
+ * @author <a href="mailto:nfilotto@exoplatform.com">Nicolas
Filotto</a>
+ * @version $Id$
+ *
+ */
+public class DataSourceProviderImpl implements DataSourceProvider
+{
+
+ /**
+ * The name of the parameter to know if the tx has to be checked or not.
+ */
+ protected static final String PARAM_CHECK_TX = "check-tx-active";
+
+ /**
+ * The name of the parameter to know if the data sources are always managed.
+ */
+ protected static final String PARAM_ALWAYS_MANAGED = "always-managed";
+
+ /**
+ * The name of the parameter of all the managed data sources.
+ */
+ protected static final String PARAM_MANAGED_DS = "managed-data-sources";
+
+ /**
+ * The transaction manager
+ */
+ protected final TransactionManager tm;
+
+ /**
+ * Indicates if the data source needs to check if a tx is active
+ * to decide if the provided connection needs to be managed or not.
+ * If it is set to false, the data source will provide only
+ * managed connections if the data source itself is managed.
+ */
+ protected boolean checkIfTxActive = true;
+
+ /**
+ * Indicates that all the data sources are managed
+ */
+ protected boolean alwaysManaged;
+
+ /**
+ * A set of all the data sources that are managed
+ */
+ protected final Set<String> managedDS = new HashSet<String>();
+
+ /**
+ * The default constructor
+ */
+ public DataSourceProviderImpl(InitParams params)
+ {
+ this(params, null);
+ }
+
+ /**
+ * The default constructor
+ */
+ public DataSourceProviderImpl(InitParams params, TransactionService tService)
+ {
+ this.tm = tService == null ? null : tService.getTransactionManager();
+ if (params != null)
+ {
+ ValueParam param = params.getValueParam(PARAM_CHECK_TX);
+ if (param != null)
+ {
+ this.checkIfTxActive = Boolean.valueOf(param.getValue());
+ }
+ param = params.getValueParam(PARAM_ALWAYS_MANAGED);
+ if (param != null && Boolean.valueOf(param.getValue()))
+ {
+ this.alwaysManaged = true;
+ return;
+ }
+ ValuesParam vp = params.getValuesParam(PARAM_MANAGED_DS);
+ if (vp != null && vp.getValues() != null)
+ {
+ for (Object oValue : vp.getValues())
+ {
+ String s = (String)oValue;
+ StringTokenizer st = new StringTokenizer(s, ",");
+ while (st.hasMoreTokens())
+ {
+ String dsName = st.nextToken().trim();
+ if (!dsName.isEmpty())
+ {
+ managedDS.add(dsName);
+ }
+ }
+ }
+ }
+ }
+ }
+
+ /**
+ * @throws NamingException
+ * @see
org.exoplatform.services.jdbc.DataSourceProvider#getDataSource(java.lang.String)
+ */
+ public DataSource getDataSource(String dataSourceName) throws NamingException
+ {
+ DataSource ds = (DataSource)new InitialContext().lookup(dataSourceName);
+ // wrap the data source object if it is managed
+ return isManaged(dataSourceName) ? new ManagedDataSource(ds, tm, checkIfTxActive) :
ds;
+ }
+
+ /**
+ * @see org.exoplatform.services.jdbc.DataSourceProvider#isManaged(java.lang.String)
+ */
+ public boolean isManaged(String dataSourceName)
+ {
+ if (alwaysManaged)
+ {
+ return true;
+ }
+ else if (managedDS.isEmpty())
+ {
+ return false;
+ }
+ return managedDS.contains(dataSourceName);
+ }
+}
Added:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/ManagedConnection.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/ManagedConnection.java
(rev 0)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/ManagedConnection.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -0,0 +1,458 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.jdbc.impl;
+
+import java.sql.Array;
+import java.sql.Blob;
+import java.sql.CallableStatement;
+import java.sql.Clob;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.NClob;
+import java.sql.PreparedStatement;
+import java.sql.SQLClientInfoException;
+import java.sql.SQLException;
+import java.sql.SQLWarning;
+import java.sql.SQLXML;
+import java.sql.Savepoint;
+import java.sql.Statement;
+import java.sql.Struct;
+import java.util.Map;
+import java.util.Properties;
+
+/**
+ * This classes wraps a jdbc connection in order to prevent any forbidden
+ * actions such as explicit commit/rollback.
+ *
+ * @author <a href="mailto:nfilotto@exoplatform.com">Nicolas
Filotto</a>
+ * @version $Id$
+ *
+ */
+public class ManagedConnection implements Connection
+{
+
+ /**
+ * The nested connection
+ */
+ private final Connection con;
+
+ /**
+ * default constructor
+ */
+ public ManagedConnection(Connection con)
+ {
+ this.con = con;
+ }
+
+ /**
+ * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
+ */
+ public boolean isWrapperFor(Class<?> iface) throws SQLException
+ {
+ return con.isWrapperFor(iface);
+ }
+
+ /**
+ * @see java.sql.Wrapper#unwrap(java.lang.Class)
+ */
+ public <T> T unwrap(Class<T> iface) throws SQLException
+ {
+ return con.unwrap(iface);
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String)
+ */
+ public PreparedStatement prepareStatement(String sql) throws SQLException
+ {
+ return con.prepareStatement(sql);
+ }
+
+ /**
+ * @see java.sql.Connection#prepareCall(java.lang.String)
+ */
+ public CallableStatement prepareCall(String sql) throws SQLException
+ {
+ return con.prepareCall(sql);
+ }
+
+ /**
+ * @see java.sql.Connection#nativeSQL(java.lang.String)
+ */
+ public String nativeSQL(String sql) throws SQLException
+ {
+ return con.nativeSQL(sql);
+ }
+
+ /**
+ * @see java.sql.Connection#setAutoCommit(boolean)
+ */
+ public void setAutoCommit(boolean autoCommit) throws SQLException
+ {
+ con.setAutoCommit(autoCommit);
+ }
+
+ /**
+ * @see java.sql.Connection#commit()
+ */
+ public void commit() throws SQLException
+ {
+ // We cannot call commit explicitly, it will be done by the AS itself
+ }
+
+ /**
+ * @see java.sql.Connection#rollback()
+ */
+ public void rollback() throws SQLException
+ {
+ // We cannot call rollback explicitly, it will be done by the AS itself
+ }
+
+ /**
+ * @see java.sql.Connection#close()
+ */
+ public void close() throws SQLException
+ {
+ con.close();
+ }
+
+ /**
+ * @see java.sql.Connection#setReadOnly(boolean)
+ */
+ public void setReadOnly(boolean readOnly) throws SQLException
+ {
+ con.setReadOnly(readOnly);
+ }
+
+ /**
+ * @see java.sql.Connection#setCatalog(java.lang.String)
+ */
+ public void setCatalog(String catalog) throws SQLException
+ {
+ con.setCatalog(catalog);
+ }
+
+ /**
+ * @see java.sql.Connection#setTransactionIsolation(int)
+ */
+ public void setTransactionIsolation(int level) throws SQLException
+ {
+ con.setTransactionIsolation(level);
+ }
+
+ /**
+ * @see java.sql.Connection#clearWarnings()
+ */
+ public void clearWarnings() throws SQLException
+ {
+ con.clearWarnings();
+ }
+
+ /**
+ * @see java.sql.Connection#createArrayOf(java.lang.String, java.lang.Object[])
+ */
+ public Array createArrayOf(String typeName, Object[] elements) throws SQLException
+ {
+ return con.createArrayOf(typeName, elements);
+ }
+
+ /**
+ * @see java.sql.Connection#createBlob()
+ */
+ public Blob createBlob() throws SQLException
+ {
+ return con.createBlob();
+ }
+
+ /**
+ * @see java.sql.Connection#createClob()
+ */
+ public Clob createClob() throws SQLException
+ {
+ return con.createClob();
+ }
+
+ /**
+ * @see java.sql.Connection#createNClob()
+ */
+ public NClob createNClob() throws SQLException
+ {
+ return con.createNClob();
+ }
+
+ /**
+ * @see java.sql.Connection#createSQLXML()
+ */
+ public SQLXML createSQLXML() throws SQLException
+ {
+ return con.createSQLXML();
+ }
+
+ /**
+ * @see java.sql.Connection#createStatement()
+ */
+ public Statement createStatement() throws SQLException
+ {
+ return con.createStatement();
+ }
+
+ /**
+ * @see java.sql.Connection#createStatement(int, int)
+ */
+ public Statement createStatement(int resultSetType, int resultSetConcurrency) throws
SQLException
+ {
+ return con.createStatement(resultSetType, resultSetConcurrency);
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
+ */
+ public PreparedStatement prepareStatement(String sql, int resultSetType, int
resultSetConcurrency)
+ throws SQLException
+ {
+ return con.prepareStatement(sql, resultSetType, resultSetConcurrency);
+ }
+
+ /**
+ * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
+ */
+ public CallableStatement prepareCall(String sql, int resultSetType, int
resultSetConcurrency) throws SQLException
+ {
+ return con.prepareCall(sql, resultSetType, resultSetConcurrency);
+ }
+
+ /**
+ * @see java.sql.Connection#setTypeMap(java.util.Map)
+ */
+ public void setTypeMap(Map<String, Class<?>> map) throws SQLException
+ {
+ con.setTypeMap(map);
+ }
+
+ /**
+ * @see java.sql.Connection#setSavepoint()
+ */
+ public Savepoint setSavepoint() throws SQLException
+ {
+ return con.setSavepoint();
+ }
+
+ /**
+ * @see java.sql.Connection#setSavepoint(java.lang.String)
+ */
+ public Savepoint setSavepoint(String name) throws SQLException
+ {
+ return con.setSavepoint(name);
+ }
+
+ /**
+ * @see java.sql.Connection#rollback(java.sql.Savepoint)
+ */
+ public void rollback(Savepoint savepoint) throws SQLException
+ {
+ // We cannot call rollback explicitly, it will be done by the AS itself
+ }
+
+ /**
+ * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
+ */
+ public void releaseSavepoint(Savepoint savepoint) throws SQLException
+ {
+ con.releaseSavepoint(savepoint);
+ }
+
+ /**
+ * @see java.sql.Connection#createStatement(int, int, int)
+ */
+ public Statement createStatement(int resultSetType, int resultSetConcurrency, int
resultSetHoldability)
+ throws SQLException
+ {
+ return con.createStatement(resultSetType, resultSetConcurrency,
resultSetHoldability);
+ }
+
+ /**
+ * @see java.sql.Connection#createStruct(java.lang.String, java.lang.Object[])
+ */
+ public Struct createStruct(String typeName, Object[] attributes) throws SQLException
+ {
+ return con.createStruct(typeName, attributes);
+ }
+
+ /**
+ * @see java.sql.Connection#getAutoCommit()
+ */
+ public boolean getAutoCommit() throws SQLException
+ {
+ return con.getAutoCommit();
+ }
+
+ /**
+ * @see java.sql.Connection#isClosed()
+ */
+ public boolean isClosed() throws SQLException
+ {
+ return con.isClosed();
+ }
+
+ /**
+ * @see java.sql.Connection#isReadOnly()
+ */
+ public boolean isReadOnly() throws SQLException
+ {
+ return con.isReadOnly();
+ }
+
+ /**
+ * @see java.sql.Connection#getCatalog()
+ */
+ public String getCatalog() throws SQLException
+ {
+ return con.getCatalog();
+ }
+
+ /**
+ * @see java.sql.Connection#getClientInfo()
+ */
+ public Properties getClientInfo() throws SQLException
+ {
+ return con.getClientInfo();
+ }
+
+ /**
+ * @see java.sql.Connection#getClientInfo(java.lang.String)
+ */
+ public String getClientInfo(String name) throws SQLException
+ {
+ return con.getClientInfo(name);
+ }
+
+ /**
+ * @see java.sql.Connection#getMetaData()
+ */
+ public DatabaseMetaData getMetaData() throws SQLException
+ {
+ return con.getMetaData();
+ }
+
+ /**
+ * @see java.sql.Connection#getTransactionIsolation()
+ */
+ public int getTransactionIsolation() throws SQLException
+ {
+ return con.getTransactionIsolation();
+ }
+
+ /**
+ * @see java.sql.Connection#getWarnings()
+ */
+ public SQLWarning getWarnings() throws SQLException
+ {
+ return con.getWarnings();
+ }
+
+ /**
+ * @see java.sql.Connection#getTypeMap()
+ */
+ public Map<String, Class<?>> getTypeMap() throws SQLException
+ {
+ return con.getTypeMap();
+ }
+
+ /**
+ * @see java.sql.Connection#getHoldability()
+ */
+ public int getHoldability() throws SQLException
+ {
+ return con.getHoldability();
+ }
+
+ /**
+ * @see java.sql.Connection#isValid(int)
+ */
+ public boolean isValid(int timeout) throws SQLException
+ {
+ return con.isValid(timeout);
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, int)
+ */
+ public PreparedStatement prepareStatement(String sql, int resultSetType, int
resultSetConcurrency,
+ int resultSetHoldability) throws SQLException
+ {
+ return con.prepareStatement(sql, resultSetType, resultSetConcurrency,
resultSetHoldability);
+ }
+
+ /**
+ * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
+ */
+ public CallableStatement prepareCall(String sql, int resultSetType, int
resultSetConcurrency,
+ int resultSetHoldability) throws SQLException
+ {
+ return con.prepareCall(sql, resultSetType, resultSetConcurrency,
resultSetHoldability);
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String, int)
+ */
+ public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws
SQLException
+ {
+ return con.prepareStatement(sql, autoGeneratedKeys);
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
+ */
+ public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws
SQLException
+ {
+ return con.prepareStatement(sql, columnIndexes);
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String, java.lang.String[])
+ */
+ public PreparedStatement prepareStatement(String sql, String[] columnNames) throws
SQLException
+ {
+ return con.prepareStatement(sql, columnNames);
+ }
+
+ /**
+ * @see java.sql.Connection#setClientInfo(java.util.Properties)
+ */
+ public void setClientInfo(Properties properties) throws SQLClientInfoException
+ {
+ con.setClientInfo(properties);
+ }
+
+ /**
+ * @see java.sql.Connection#setClientInfo(java.lang.String, java.lang.String)
+ */
+ public void setClientInfo(String name, String value) throws SQLClientInfoException
+ {
+ con.setClientInfo(name, value);
+ }
+
+ /**
+ * @see java.sql.Connection#setHoldability(int)
+ */
+ public void setHoldability(int holdability) throws SQLException
+ {
+ con.setHoldability(holdability);
+ }
+}
Added:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/ManagedDataSource.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/ManagedDataSource.java
(rev 0)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/jdbc/impl/ManagedDataSource.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -0,0 +1,162 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.jdbc.impl;
+
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+
+import javax.sql.DataSource;
+import javax.transaction.Status;
+import javax.transaction.TransactionManager;
+
+/**
+ * This classes is used to wrap the original {@link DataSource}
+ * in order to be able to support the managed data sources with a limited
+ * amount of changes. A {@link DataSource} is expected to be wrapped only
+ * when a data source is defined and has been configured as managed
+ *
+ * @author <a href="mailto:nfilotto@exoplatform.com">Nicolas
Filotto</a>
+ * @version $Id$
+ *
+ */
+public class ManagedDataSource implements DataSource
+{
+ private static final Log LOG =
ExoLogger.getLogger("exo.jcr.component.core.ManagedDataSource");
+
+ /**
+ * The transaction manager
+ */
+ private final TransactionManager tm;
+
+ /**
+ * The wrapped {@link DataSource}
+ */
+ private final DataSource ds;
+
+ /**
+ * Indicates whether the tx status need to be check first to know
+ * if the provided connection needs to be managed or not
+ */
+ private final boolean checkIfTxActive;
+
+ /**
+ * default constructor
+ */
+ public ManagedDataSource(DataSource ds, TransactionManager tm, boolean
checkIfTxActive)
+ {
+ this.tm = tm;
+ this.ds = ds;
+ this.checkIfTxActive = checkIfTxActive;
+ }
+
+ /**
+ * @see javax.sql.CommonDataSource#getLogWriter()
+ */
+ public PrintWriter getLogWriter() throws SQLException
+ {
+ return ds.getLogWriter();
+ }
+
+ /**
+ * @see javax.sql.CommonDataSource#getLoginTimeout()
+ */
+ public int getLoginTimeout() throws SQLException
+ {
+ return ds.getLoginTimeout();
+ }
+
+ /**
+ * @see javax.sql.CommonDataSource#setLogWriter(java.io.PrintWriter)
+ */
+ public void setLogWriter(PrintWriter out) throws SQLException
+ {
+ ds.setLogWriter(out);
+ }
+
+ /**
+ * @see javax.sql.CommonDataSource#setLoginTimeout(int)
+ */
+ public void setLoginTimeout(int seconds) throws SQLException
+ {
+ ds.setLoginTimeout(seconds);
+ }
+
+ /**
+ * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
+ */
+ public boolean isWrapperFor(Class<?> iface) throws SQLException
+ {
+ return ds.isWrapperFor(iface);
+ }
+
+ /**
+ * @see java.sql.Wrapper#unwrap(java.lang.Class)
+ */
+ public <T> T unwrap(Class<T> iface) throws SQLException
+ {
+ return ds.unwrap(iface);
+ }
+
+ /**
+ * @see javax.sql.DataSource#getConnection()
+ */
+ public Connection getConnection() throws SQLException
+ {
+ Connection con = ds.getConnection();
+ return providesManagedConnection() ? new ManagedConnection(con) : con;
+ }
+
+ /**
+ * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
+ */
+ public Connection getConnection(String username, String password) throws SQLException
+ {
+ Connection con = ds.getConnection(username, password);
+ return providesManagedConnection() ? new ManagedConnection(con) : con;
+ }
+
+ /**
+ * Indicates whether or not a global tx is active
+ * @return <code>true</code> if a tx is active,
<code>false</code> otherwise
+ */
+ private boolean isTxActive()
+ {
+ try
+ {
+ return tm != null && tm.getStatus() != Status.STATUS_NO_TRANSACTION;
+ }
+ catch (Exception e)
+ {
+ LOG.warn("We cannot know if a global tx is active", e);
+ }
+ return false;
+ }
+
+ /**
+ * Indicates whether or not the provided connection needs to be managed
+ */
+ private boolean providesManagedConnection()
+ {
+ return !checkIfTxActive || isTxActive();
+ }
+}
Modified:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/ExoResource.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/ExoResource.java 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/ExoResource.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -1,64 +0,0 @@
-/*
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.exoplatform.services.transaction;
-
-import javax.transaction.xa.XAException;
-import javax.transaction.xa.XAResource;
-
-/**
- * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
- * @version $Revision$
- */
-public interface ExoResource
-{
-
- /**
- * @return XAResource
- */
- XAResource getXAResource();
-
- /**
- * Enlists XAResource in TM.
- *
- * @throws javax.transaction.xa.XAException
- */
- void enlistResource() throws XAException;
-
- /**
- * Delists XAResource in TM.
- *
- * @throws XAException
- */
- void delistResource() throws XAException;
-
- /**
- * Returns the payload attached to the resource.
- *
- * @return the payload
- */
- Object getPayload();
-
- /**
- * Attach a payload to the resource.
- *
- * @param payload the payload
- */
- void setPayload(Object payload);
-
-}
Modified:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionException.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionException.java 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionException.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -1,76 +0,0 @@
-/*
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.exoplatform.services.transaction;
-
-import javax.transaction.xa.XAException;
-
-/**
- * Created by The eXo Platform SAS.
- *
- * @author <a href="mailto:gennady.azarenkov@exoplatform.com">Gennady
- * Azarenkov</a>
- * @version $Id: TransactionException.java 5987 2006-06-05 10:14:23Z geaz $
- */
-
-public class TransactionException extends Exception
-{
-
- private final int errcode;
-
- public TransactionException()
- {
- super();
- this.errcode = XAException.XA_RBOTHER;
- }
-
- public TransactionException(String arg0)
- {
- super(arg0);
- this.errcode = XAException.XA_RBOTHER;
- }
-
- public TransactionException(String arg0, Throwable arg1)
- {
- super(arg0, arg1);
- this.errcode = XAException.XA_RBOTHER;
- }
-
- public TransactionException(Throwable arg0)
- {
- super(arg0);
- this.errcode = XAException.XA_RBOTHER;
- }
-
- public TransactionException(int errcode, String arg0)
- {
- super(arg0);
- this.errcode = errcode;
- }
-
- public TransactionException(int errcode, Throwable arg0)
- {
- super(arg0);
- this.errcode = errcode;
- }
-
- public int getErrorCode()
- {
- return errcode;
- }
-}
Modified:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionResource.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionResource.java 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionResource.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -1,57 +0,0 @@
-/*
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.exoplatform.services.transaction;
-
-/**
- * Created by The eXo Platform SAS.<br/> Internal resource manager for support
- * transaction
- *
- * @author <a href="mailto:gennady.azarenkov@exoplatform.com">Gennady
- * Azarenkov</a>
- * @version $Id: TransactionResource.java 6853 2006-07-07 11:41:24Z geaz $
- */
-
-public interface TransactionResource
-{
-
- /**
- * called when transaction is starting (by
- * javax.transaction.xa.XAResource#start(javax.transaction.xa.Xid, int))
- *
- * @throws TransactionException
- */
- void start() throws TransactionException;
-
- /**
- * called when transaction is committing (by
- * javax.transaction.xa.XAResource#commit(javax.transaction.xa.Xid, boolean))
- *
- * @throws TransactionException
- */
- void commit() throws TransactionException;
-
- /**
- * called when transaction is rolling back (by
- * javax.transaction.xa.XAResource#rollback(javax.transaction.xa.Xid))
- *
- * @throws TransactionException
- */
- void rollback() throws TransactionException;
-
-}
Modified:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionService.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionService.java 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/TransactionService.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -22,7 +22,7 @@
import javax.transaction.SystemException;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
-import javax.transaction.xa.Xid;
+import javax.transaction.xa.XAResource;
/**
* Created by The eXo Platform SAS.<br/> The transaction service
@@ -62,25 +62,34 @@
* Enlists XA resource in transaction manager.
*
* @param xares XAResource
- * @throws RollbackException
- * @throws SystemException
+ * @return <i>true</i> if the resource was enlisted successfully;
otherwise
+ * <i>false</i>.
+ *
+ * @exception RollbackException Thrown to indicate that
+ * the transaction has been marked for rollback only.
+ *
+ * @exception IllegalStateException Thrown if the transaction in the
+ * target object is in the prepared state or the transaction is
+ * inactive.
+ *
+ * @exception SystemException Thrown if the transaction manager
+ * encounters an unexpected error condition.
*/
- void enlistResource(ExoResource xares) throws RollbackException, SystemException;
+ boolean enlistResource(XAResource xares) throws RollbackException, SystemException,
IllegalStateException;
/**
* Delists XA resource from transaction manager.
*
* @param xares XAResource
- * @throws RollbackException
- * @throws SystemException
+ * @exception IllegalStateException Thrown if the transaction in the
+ * target object is inactive.
+ *
+ * @exception SystemException Thrown if the transaction manager
+ * encounters an unexpected error condition.
+ *
+ * @return <i>true</i> if the resource was delisted successfully;
otherwise
+ * <i>false</i>.
*/
- void delistResource(ExoResource xares) throws RollbackException, SystemException;
+ boolean delistResource(XAResource xares) throws RollbackException, SystemException,
IllegalStateException;
- /**
- * Creates unique XA transaction identifier.
- *
- * @return Xid
- */
- Xid createXid();
-
}
Added:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/AbstractTransactionService.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/AbstractTransactionService.java
(rev 0)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/AbstractTransactionService.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -0,0 +1,845 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.transaction.impl;
+
+import org.exoplatform.commons.utils.SecurityHelper;
+import org.exoplatform.container.xml.InitParams;
+import org.exoplatform.services.log.ExoLogger;
+import org.exoplatform.services.log.Log;
+import org.exoplatform.services.transaction.TransactionService;
+
+import java.security.AccessController;
+import java.security.PrivilegedActionException;
+import java.security.PrivilegedExceptionAction;
+
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.InvalidTransactionException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+import javax.transaction.UserTransaction;
+import javax.transaction.xa.XAResource;
+
+/**
+ * This abstract class implements the main logic of all the methods expected for a
+ * {@link TransactionService}. If you intend to use a {@link TransactionManager} in
+ * standalone mode (not manager by your Application Server), you can set the
+ * transaction timeout thanks to the <code>value-param</code> called
<code>timeout</code>
+ * the value of this parameter is expressed in seconds.
+ *
+ * @author <a href="mailto:nicolas.filotto@exoplatform.com">Nicolas
Filotto</a>
+ * @version $Id$
+ *
+ */
+public abstract class AbstractTransactionService implements TransactionService
+{
+ /**
+ * The logger
+ */
+ private static final Log LOG =
ExoLogger.getLogger("exo.kernel.component.common.AbstractTransactionService");
+
+ /**
+ * The default value of a transaction timeout in seconds
+ */
+ private static final int DEFAULT_TIME_OUT = 60;
+
+ /**
+ * The default timeout
+ */
+ protected final int defaultTimeout;
+
+ /**
+ * Indicates if the timeout has to be enforced
+ */
+ protected final boolean forceTimeout;
+
+ /**
+ * The current Transaction Manager
+ */
+ private volatile TransactionManager tm;
+
+ /**
+ * The current User Transaction
+ */
+ private volatile UserTransaction ut;
+
+ public AbstractTransactionService()
+ {
+ this(null);
+ }
+
+ public AbstractTransactionService(InitParams params)
+ {
+ if (params != null && params.getValueParam("timeout") != null)
+ {
+ this.defaultTimeout =
Integer.parseInt(params.getValueParam("timeout").getValue());
+ this.forceTimeout = true;
+ }
+ else
+ {
+ this.defaultTimeout = DEFAULT_TIME_OUT;
+ this.forceTimeout = false;
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean delistResource(final XAResource xares) throws RollbackException,
SystemException
+ {
+ TransactionManager tm = getTransactionManager();
+ final Transaction tx = tm.getTransaction();
+ if (tx != null)
+ {
+ PrivilegedExceptionAction<Boolean> action = new
PrivilegedExceptionAction<Boolean>()
+ {
+ public Boolean run() throws Exception
+ {
+ int flag = XAResource.TMSUCCESS;
+ switch (tx.getStatus())
+ {
+ case Status.STATUS_MARKED_ROLLBACK:
+ case Status.STATUS_ROLLEDBACK:
+ case Status.STATUS_ROLLING_BACK: flag = XAResource.TMFAIL;
+ }
+ return tx.delistResource(xares, flag);
+ }
+ };
+ try
+ {
+ return AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+
+ if (cause instanceof RollbackException)
+ {
+ throw (RollbackException)cause;
+ }
+ else if (cause instanceof IllegalStateException)
+ {
+ throw (IllegalStateException)cause;
+ }
+ else if (cause instanceof SystemException)
+ {
+ throw (SystemException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+ else
+ {
+ throw new IllegalStateException("Could not delist the XA Resource since
there is no active session");
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public boolean enlistResource(final XAResource xares) throws RollbackException,
SystemException
+ {
+ TransactionManager tm = getTransactionManager();
+ final Transaction tx = tm.getTransaction();
+ if (tx != null)
+ {
+ PrivilegedExceptionAction<Boolean> action = new
PrivilegedExceptionAction<Boolean>()
+ {
+ public Boolean run() throws Exception
+ {
+ return tx.enlistResource(xares);
+ }
+ };
+ try
+ {
+ return AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+
+ if (cause instanceof RollbackException)
+ {
+ throw (RollbackException)cause;
+ }
+ else if (cause instanceof IllegalStateException)
+ {
+ throw (IllegalStateException)cause;
+ }
+ else if (cause instanceof SystemException)
+ {
+ throw (SystemException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+ else
+ {
+ throw new IllegalStateException("Could not enlist the XA Resource since
there is no active session");
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getDefaultTimeout()
+ {
+ return defaultTimeout;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public final TransactionManager getTransactionManager()
+ {
+ if (tm == null)
+ {
+ synchronized (this)
+ {
+ if (tm == null)
+ {
+ TransactionManager tm;
+ try
+ {
+ tm = SecurityHelper.doPrivilegedExceptionAction(new
PrivilegedExceptionAction<TransactionManager>()
+ {
+ public TransactionManager run() throws Exception
+ {
+ return findTransactionManager();
+ }
+ });
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("Transaction manager not found",
e);
+ }
+ if (forceTimeout)
+ {
+ // Only set the timeout when a timeout has been given into the
+ // configuration otherwise we assume that the value will be
+ // set at the AS level
+ tm = new TransactionManagerTxTimeoutAware(tm, defaultTimeout);
+ }
+ this.tm = tm;
+ }
+ }
+ }
+ return tm;
+ }
+
+ /**
+ * Indicates whether or not the {@link TransactionManager} has been initialized
+ */
+ protected boolean isTMInitialized()
+ {
+ return tm != null;
+ }
+
+ /**
+ * This method will try to find the current {@link TransactionManager}
+ * @return the current {@link TransactionManager}
+ * @throws Exception if an error occurs while looking for the {@link
TransactionManager}
+ */
+ protected abstract TransactionManager findTransactionManager() throws Exception;
+
+ /**
+ * {@inheritDoc}
+ */
+ public final UserTransaction getUserTransaction()
+ {
+ if (ut == null)
+ {
+ synchronized (this)
+ {
+ if (ut == null)
+ {
+ UserTransaction ut;
+ try
+ {
+ ut = SecurityHelper.doPrivilegedExceptionAction(new
PrivilegedExceptionAction<UserTransaction>()
+ {
+ public UserTransaction run() throws Exception
+ {
+ return findUserTransaction();
+ }
+ });
+ }
+ catch (Exception e)
+ {
+ throw new RuntimeException("UserTransaction not found", e);
+ }
+ this.ut = ut;
+ }
+ }
+ }
+ return ut;
+ }
+
+
+ /**
+ * This method will try to find the current {@link UserTransaction}, by default it
will
+ * simply wraps a {@link TransactionManager}
+ * @return the current {@link UserTransaction}
+ * @throws Exception if an error occurs while looking for the {@link UserTransaction}
+ */
+ protected UserTransaction findUserTransaction() throws Exception
+ {
+ return new UserTransactionWrapper(getTransactionManager());
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setTransactionTimeout(int seconds) throws SystemException
+ {
+ TransactionManager tm = getTransactionManager();
+ tm.setTransactionTimeout(seconds);
+ }
+
+ /**
+ * This class is used to enforce the {@link Transaction} timeout when a new
transaction is
+ * created through the nested {@link TransactionManager}
+ *
+ * Created by The eXo Platform SAS
+ * Author : Nicolas Filotto
+ * nicolas.filotto(a)exoplatform.com
+ * 1 f�vr. 2010
+ */
+ private static class TransactionManagerTxTimeoutAware implements TransactionManager
+ {
+ /**
+ * The nested {@link TransactionManager}
+ */
+ private final TransactionManager tm;
+
+ /**
+ * The default timeout of the {@link Transaction}
+ */
+ private final int defaultTimeout;
+
+ /**
+ * This is used to know if a timeout has already been set for the next transaction
+ */
+ private final ThreadLocal<Boolean> timeoutHasBeenSet = new
ThreadLocal<Boolean>();
+
+ public TransactionManagerTxTimeoutAware(TransactionManager tm, int defaultTimeout)
+ {
+ this.tm = tm;
+ this.defaultTimeout = defaultTimeout;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void begin() throws NotSupportedException, SystemException
+ {
+ if (timeoutHasBeenSet.get() != null)
+ {
+ // clean the ThreadLocal
+ timeoutHasBeenSet.set(null);
+ }
+ else
+ {
+ try
+ {
+ // Set the default transaction timeout
+ tm.setTransactionTimeout(defaultTimeout);
+ }
+ catch (Exception e)
+ {
+ LOG.warn("Cannot set the transaction timeout", e);
+ }
+ }
+
+ // Start the transaction
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ tm.begin();
+ return null;
+ }
+ };
+ try
+ {
+ AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+ if (cause instanceof NotSupportedException)
+ {
+ throw (NotSupportedException)cause;
+ }
+ else if (cause instanceof SystemException)
+ {
+ throw (SystemException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void commit() throws RollbackException, HeuristicMixedException,
HeuristicRollbackException,
+ SecurityException, IllegalStateException, SystemException
+ {
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ tm.commit();
+ return null;
+ }
+ };
+ try
+ {
+ AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+ if (cause instanceof RollbackException)
+ {
+ throw (RollbackException)cause;
+ }
+ else if (cause instanceof HeuristicMixedException)
+ {
+ throw (HeuristicMixedException)cause;
+ }
+ else if (cause instanceof HeuristicRollbackException)
+ {
+ throw (HeuristicRollbackException)cause;
+ }
+ else if (cause instanceof SecurityException)
+ {
+ throw (SecurityException)cause;
+ }
+ else if (cause instanceof IllegalStateException)
+ {
+ throw (IllegalStateException)cause;
+ }
+ else if (cause instanceof SystemException)
+ {
+ throw (SystemException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public int getStatus() throws SystemException
+ {
+ return tm.getStatus();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Transaction getTransaction() throws SystemException
+ {
+ try
+ {
+ return SecurityHelper.doPrivilegedExceptionAction(new
PrivilegedExceptionAction<Transaction>()
+ {
+ public Transaction run() throws Exception
+ {
+ return tm.getTransaction();
+ }
+ });
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+ if (cause instanceof SystemException)
+ {
+ throw (SystemException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void resume(final Transaction tx) throws InvalidTransactionException,
IllegalStateException,
+ SystemException
+ {
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ tm.resume(tx);
+ return null;
+ }
+ };
+ try
+ {
+ AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+ if (cause instanceof InvalidTransactionException)
+ {
+ throw (InvalidTransactionException)cause;
+ }
+ else if (cause instanceof IllegalStateException)
+ {
+ throw (IllegalStateException)cause;
+ }
+ else if (cause instanceof SystemException)
+ {
+ throw (SystemException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void rollback() throws IllegalStateException, SecurityException,
SystemException
+ {
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ tm.rollback();
+ return null;
+ }
+ };
+ try
+ {
+ AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+ if (cause instanceof IllegalStateException)
+ {
+ throw (IllegalStateException)cause;
+ }
+ else if (cause instanceof SecurityException)
+ {
+ throw (SecurityException)cause;
+ }
+ else if (cause instanceof SystemException)
+ {
+ throw (SystemException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setRollbackOnly() throws IllegalStateException, SystemException
+ {
+ tm.setRollbackOnly();
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public void setTransactionTimeout(int timeout) throws SystemException
+ {
+ tm.setTransactionTimeout(timeout);
+ timeoutHasBeenSet.set(true);
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ public Transaction suspend() throws SystemException
+ {
+ PrivilegedExceptionAction<Transaction> action = new
PrivilegedExceptionAction<Transaction>()
+ {
+ public Transaction run() throws Exception
+ {
+ return tm.suspend();
+ }
+ };
+ try
+ {
+ return AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+ if (cause instanceof SystemException)
+ {
+ throw (SystemException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+ }
+
+ /**
+ * This class is used to propose a default implementation of a {@link
UserTransaction}
+ * from the {@link TransactionManager}
+ * @author <a href="mailto:nfilotto@exoplatform.com">Nicolas
Filotto</a>
+ * @version $Id$
+ *
+ */
+ private static class UserTransactionWrapper implements UserTransaction
+ {
+
+ /**
+ * The {@link TransactionManager} that we will use to simulate a {@link
UserTransaction}
+ */
+ private final TransactionManager tm;
+
+ /**
+ * Default Constructor
+ * @param tm
+ */
+ public UserTransactionWrapper(TransactionManager tm)
+ {
+ this.tm = tm;
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#begin()
+ */
+ public void begin() throws NotSupportedException, SystemException
+ {
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ tm.begin();
+ return null;
+ }
+ };
+ try
+ {
+ AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+ if (cause instanceof NotSupportedException)
+ {
+ throw (NotSupportedException)cause;
+ }
+ else if (cause instanceof SystemException)
+ {
+ throw (SystemException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#commit()
+ */
+ public void commit() throws RollbackException, HeuristicMixedException,
HeuristicRollbackException,
+ SecurityException, IllegalStateException, SystemException
+ {
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ tm.commit();
+ return null;
+ }
+ };
+ try
+ {
+ AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+ if (cause instanceof RollbackException)
+ {
+ throw (RollbackException)cause;
+ }
+ else if (cause instanceof HeuristicMixedException)
+ {
+ throw (HeuristicMixedException)cause;
+ }
+ else if (cause instanceof HeuristicRollbackException)
+ {
+ throw (HeuristicRollbackException)cause;
+ }
+ else if (cause instanceof SecurityException)
+ {
+ throw (SecurityException)cause;
+ }
+ else if (cause instanceof IllegalStateException)
+ {
+ throw (IllegalStateException)cause;
+ }
+ else if (cause instanceof SystemException)
+ {
+ throw (SystemException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#rollback()
+ */
+ public void rollback() throws IllegalStateException, SecurityException,
SystemException
+ {
+ PrivilegedExceptionAction<Object> action = new
PrivilegedExceptionAction<Object>()
+ {
+ public Object run() throws Exception
+ {
+ tm.rollback();
+ return null;
+ }
+ };
+ try
+ {
+ AccessController.doPrivileged(action);
+ }
+ catch (PrivilegedActionException pae)
+ {
+ Throwable cause = pae.getCause();
+ if (cause instanceof IllegalStateException)
+ {
+ throw (IllegalStateException)cause;
+ }
+ else if (cause instanceof SecurityException)
+ {
+ throw (SecurityException)cause;
+ }
+ else if (cause instanceof SystemException)
+ {
+ throw (SystemException)cause;
+ }
+ else if (cause instanceof RuntimeException)
+ {
+ throw (RuntimeException)cause;
+ }
+ else
+ {
+ throw new RuntimeException(cause);
+ }
+ }
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#setRollbackOnly()
+ */
+ public void setRollbackOnly() throws IllegalStateException, SystemException
+ {
+ tm.setRollbackOnly();
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#getStatus()
+ */
+ public int getStatus() throws SystemException
+ {
+ return tm.getStatus();
+ }
+
+ /**
+ * @see javax.transaction.UserTransaction#setTransactionTimeout(int)
+ */
+ public void setTransactionTimeout(int timeout) throws SystemException
+ {
+ tm.setTransactionTimeout(timeout);
+ }
+ }
+}
Modified:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jboss/JBossTransactionService.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jboss/JBossTransactionService.java 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jboss/JBossTransactionService.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -18,53 +18,47 @@
*/
package org.exoplatform.services.transaction.impl.jboss;
-import org.exoplatform.services.transaction.ExoResource;
-import org.exoplatform.services.transaction.TransactionService;
+import org.exoplatform.services.transaction.impl.AbstractTransactionService;
import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import javax.naming.InitialContext;
import javax.naming.NamingException;
-import javax.transaction.RollbackException;
-import javax.transaction.SystemException;
-import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
-import javax.transaction.xa.XAResource;
-import javax.transaction.xa.Xid;
/**
* @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
* @version $Revision$
*/
-public class JBossTransactionService implements TransactionService
+public class JBossTransactionService extends AbstractTransactionService
{
- /** . */
- private volatile TransactionManager tm;
-
- public TransactionManager getTransactionManager()
+ /**
+ * {@inheritDoc}
+ */
+ public TransactionManager findTransactionManager()
{
- if (tm == null)
+ try
{
- try
- {
- tm = (TransactionManager)new
InitialContext().lookup("java:/TransactionManager");
- }
- catch (NamingException e)
- {
- throw new IllegalStateException(e);
- }
+ return (TransactionManager)new
InitialContext().lookup("java:/TransactionManager");
}
- return tm;
+ catch (NamingException e)
+ {
+ throw new IllegalStateException(e);
+ }
}
- public UserTransaction getUserTransaction()
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public UserTransaction findUserTransaction()
{
try
{
- return (UserTransaction)new
InitialContext().lookup("java:/TransactionManager");
+ return (UserTransaction)new
InitialContext().lookup("java:comp/UserTransaction");
}
catch (NamingException e)
{
@@ -72,6 +66,10 @@
}
}
+ /**
+ * {@inheritDoc}
+ */
+ @Override
public int getDefaultTimeout()
{
try
@@ -85,32 +83,4 @@
throw new IllegalStateException(e);
}
}
-
- public void setTransactionTimeout(int seconds) throws SystemException
- {
- TransactionManager tm = getTransactionManager();
- tm.setTransactionTimeout(seconds);
- }
-
- public void enlistResource(ExoResource exores) throws RollbackException,
SystemException
- {
- TransactionManager tm = getTransactionManager();
- Transaction tx = tm.getTransaction();
- tx.enlistResource(exores.getXAResource());
- }
-
- public void delistResource(ExoResource exores) throws RollbackException,
SystemException
- {
- TransactionManager tm = getTransactionManager();
- Transaction tx = tm.getTransaction();
- tx.delistResource(exores.getXAResource(), XAResource.TMNOFLAGS);
- }
-
- public Xid createXid()
- {
- // Convenient method used by JCR tests to manufacture an xid
- // it should be removed
- //
- throw new UnsupportedOperationException("Only used by JCR impl in
tests");
- }
}
Modified:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jotm/ResourceEntry.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jotm/ResourceEntry.java 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jotm/ResourceEntry.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -1,66 +0,0 @@
-/*
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.exoplatform.services.transaction.impl.jotm;
-
-import org.exoplatform.services.transaction.ExoResource;
-import org.objectweb.transaction.jta.ResourceManagerEvent;
-
-import java.util.List;
-
-import javax.transaction.SystemException;
-import javax.transaction.Transaction;
-import javax.transaction.xa.XAException;
-
-/**
- * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
- * @version $Revision$
- */
-public class ResourceEntry implements ResourceManagerEvent
-{
-
- List<?> jotmResourceList;
-
- final ExoResource resource;
-
- public ResourceEntry(ExoResource resource)
- {
- this.resource = resource;
- }
-
- public void enlistConnection(Transaction transaction) throws SystemException
- {
- try
- {
- /*
- if (LOG.isDebugEnabled())
- LOG.debug("Enlist connection. Session: " + getSessionInfo() +
", " + this
- + ", transaction: " + transaction);
- */
- resource.enlistResource();
- }
- catch (IllegalStateException e)
- {
- throw new SystemException(e.getMessage());
- }
- catch (XAException e)
- {
- throw new SystemException(e.getMessage());
- }
- }
-}
Modified:
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jotm/TransactionServiceJotmImpl.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jotm/TransactionServiceJotmImpl.java 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/main/java/org/exoplatform/services/transaction/impl/jotm/TransactionServiceJotmImpl.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -23,25 +23,17 @@
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
import org.exoplatform.services.naming.InitialContextInitializer;
-import org.exoplatform.services.transaction.ExoResource;
-import org.exoplatform.services.transaction.TransactionService;
+import org.exoplatform.services.transaction.impl.AbstractTransactionService;
import org.objectweb.jotm.Current;
import org.objectweb.jotm.TransactionFactory;
import org.objectweb.jotm.TransactionFactoryImpl;
-import org.objectweb.jotm.XidImpl;
import java.rmi.RemoteException;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
-import java.util.List;
-import javax.transaction.RollbackException;
-import javax.transaction.SystemException;
-import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
-import javax.transaction.xa.XAResource;
-import javax.transaction.xa.Xid;
/**
* Created by The eXo Platform SAS.<br/> JOTM based implementation of
@@ -51,31 +43,47 @@
* Azarenkov</a>
* @version $Id: $
*/
-public class TransactionServiceJotmImpl implements TransactionService
+public class TransactionServiceJotmImpl extends AbstractTransactionService
{
protected static Log log =
ExoLogger.getLogger("exo.kernel.component.common.TransactionServiceJotmImpl");
- public static final String TRACK_WITHOT_TRANSACTION_PARAM =
"track-without-transaction";
+ private final int defaultTimeout;
+
+ /**
+ * Default constructor
+ * @param initializer we enforce a dependency with the InitialContextInitializer to
+ * ensure that the related binded resources have been defined
+ * @param params the init parameters
+ */
+ public TransactionServiceJotmImpl(InitialContextInitializer initializer, InitParams
params)
+ {
+ if (params != null && params.getValueParam("timeout") != null)
+ {
+ this.defaultTimeout =
Integer.parseInt(params.getValueParam("timeout").getValue());
+ }
+ else
+ {
+ this.defaultTimeout = -1;
+ }
+ }
- private boolean trackWithoutTransaction = false;
-
- private Current current;
-
- public TransactionServiceJotmImpl(InitialContextInitializer initializer, InitParams
params) throws RemoteException
+ /**
+ * {@inheritDoc}
+ */
+ public TransactionManager findTransactionManager() throws Exception
{
- current = Current.getCurrent();
+ Current current = Current.getCurrent();
if (current == null)
{
try
{
- SecurityHelper.doPrivilegedExceptionAction(new
PrivilegedExceptionAction<Void>()
+ current = SecurityHelper.doPrivilegedExceptionAction(new
PrivilegedExceptionAction<Current>()
{
- public Void run() throws Exception
+ public Current run() throws Exception
{
TransactionFactory tm = new TransactionFactoryImpl();
- current = new Current(tm);
- return null;
+ return new Current(tm);
}
});
}
@@ -96,144 +104,33 @@
}
}
-
// Change the timeout only if JOTM is not initialized yet
- if (params != null)
+ if (defaultTimeout > 0)
{
- if (params.getValueParam("timeout") != null)
- {
-
- int t =
Integer.parseInt(params.getValueParam("timeout").getValue());
- current.setDefaultTimeout(t);
- }
-
- if (params.getValueParam(TRACK_WITHOT_TRANSACTION_PARAM) != null)
- {
- trackWithoutTransaction =
-
Boolean.parseBoolean(params.getValueParam(TRACK_WITHOT_TRANSACTION_PARAM).getValue());
- }
+ current.setDefaultTimeout(defaultTimeout);
}
}
else
{
log.info("Use externally initialized JOTM: " + current);
}
- }
-
- /**
- * {@inheritDoc}
- */
- public TransactionManager getTransactionManager()
- {
return current;
}
/**
* {@inheritDoc}
*/
- public UserTransaction getUserTransaction()
+ public UserTransaction findUserTransaction()
{
- return current;
+ return (UserTransaction)getTransactionManager();
}
/**
* {@inheritDoc}
*/
- public void enlistResource(ExoResource exores) throws RollbackException,
SystemException
- {
- XAResource xares = exores.getXAResource();
- ResourceEntry entry = new ResourceEntry(exores);
- exores.setPayload(entry);
- Transaction tx = getTransactionManager().getTransaction();
- if (tx != null)
- {
- current.getTransaction().enlistResource(xares);
- }
- else if (trackWithoutTransaction)
- {
- current.connectionOpened(entry);
-
- // actual only if current.connectionOpened(entry);
- // otherwise NPE inside the JOTM's Current
- entry.jotmResourceList = popThreadLocalRMEventList();
- pushThreadLocalRMEventList(entry.jotmResourceList);
- }
- }
-
- /**
- * {@inheritDoc}
- */
- public void delistResource(ExoResource exores) throws RollbackException,
SystemException
- {
- XAResource xares = exores.getXAResource();
- ResourceEntry entry = (ResourceEntry)exores.getPayload();
- Transaction tx = getTransactionManager().getTransaction();
- if (tx != null)
- {
- current.getTransaction().delistResource(xares, XAResource.TMNOFLAGS);
- }
- else if (trackWithoutTransaction)
- {
- current.connectionClosed(entry);
-
- // actual only if current.connectionClosed(entry);
- if (entry != null && entry.jotmResourceList != null)
- {
- entry.jotmResourceList.remove(xares);
- }
- }
-
- exores.setPayload(null);
- }
-
- /**
- * {@inheritDoc}
- */
- public Xid createXid()
- {
- return new XidImpl();
- }
-
- /**
- * {@inheritDoc}
- */
+ @Override
public int getDefaultTimeout()
{
- return current.getDefaultTimeout();
+ return ((Current)getTransactionManager()).getDefaultTimeout();
}
-
- /**
- * {@inheritDoc}
- */
- public void setTransactionTimeout(int seconds) throws SystemException
- {
- current.setTransactionTimeout(seconds);
- }
-
- /**
- * Push a new event list on the stack of thread local resource event sets. The
- * list must contain only <code>ResourceManagerEvent</code> objects.
- *
- * @param eventList the possibly null list of events to store forecoming
- * <code>ResourceManagerEvent</code> events occuring in the
current
- * thread.
- */
- public void pushThreadLocalRMEventList(List eventList)
- {
- current.pushThreadLocalRMEventList(eventList);
- };
-
- /**
- * Pop the current set from the stack of thread local resource event sets The
- * list contains <code>ResourceManagerEvent</code> objects.
- *
- * @return The possibly null <code>ResourceManagerEvent</code> list of
events
- * that have occured in the current thread since the last call of
- * <code>pushThreadLocalRMEventList</code> or since the thread
- * started.
- */
- public List popThreadLocalRMEventList()
- {
- return current.popThreadLocalRMEventList();
- };
}
Modified:
kernel/trunk/exo.kernel.component.common/src/main/resources/conf/portal/configuration.xml
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/main/resources/conf/portal/configuration.xml 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/main/resources/conf/portal/configuration.xml 2011-05-23
06:57:15 UTC (rev 4401)
@@ -43,7 +43,39 @@
</properties-param>
</init-params>
</component>
-
+
+ <component>
+ <key>org.exoplatform.services.jdbc.DataSourceProvider</key>
+ <type>org.exoplatform.services.jdbc.impl.DataSourceProviderImpl</type>
+ <init-params>
+ <!-- Indicates that the data source needs to check if a tx is active
+ to decide if the provided connection needs to be managed or not.
+ If it is set to false, the data source will provide only
+ managed connections if the data source itself is managed. -->
+ <!--value-param>
+ <name>check-tx-active</name>
+ <value>true</value>
+ </value-param-->
+ <!-- Indicates that all the data sources are managed
+ If set to true the parameter never-managed and
+ managed-data-sources will be ignored -->
+ <!--value-param>
+ <name>always-managed</name>
+ <value>true</value>
+ </value-param-->
+ <!-- Indicates the list of all the data sources that are
+ managed, each value tag can contain a list of
+ data source names separated by a comma, in the
+ example below we will register ds-foo1, ds-foo2
+ and ds-foo3 as managed data source. If always-managed
+ and/or never-managed is set true this parameter is ignored -->
+ <!--values-param>
+ <name>managed-data-sources</name>
+ <value>ds-foo1, ds-foo2</value>
+ <value>ds-foo3</value>
+ </values-param-->
+ </init-params>
+ </component>
<!-- component>
<key>org.exoplatform.services.net.NetService</key>
<type>org.exoplatform.services.net.impl.NetServiceImpl</type>
Added:
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/jdbc/impl/TestDataSourceProviderImpl.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/jdbc/impl/TestDataSourceProviderImpl.java
(rev 0)
+++
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/jdbc/impl/TestDataSourceProviderImpl.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -0,0 +1,809 @@
+/*
+ * Copyright (C) 2011 eXo Platform SAS.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.exoplatform.services.jdbc.impl;
+
+import org.exoplatform.container.xml.InitParams;
+import org.exoplatform.container.xml.ValueParam;
+import org.exoplatform.container.xml.ValuesParam;
+import org.exoplatform.services.jdbc.DataSourceProvider;
+import org.exoplatform.services.transaction.TransactionService;
+import org.exoplatform.test.BasicTestCase;
+
+import java.io.PrintWriter;
+import java.sql.Array;
+import java.sql.Blob;
+import java.sql.CallableStatement;
+import java.sql.Clob;
+import java.sql.Connection;
+import java.sql.DatabaseMetaData;
+import java.sql.NClob;
+import java.sql.PreparedStatement;
+import java.sql.SQLClientInfoException;
+import java.sql.SQLException;
+import java.sql.SQLWarning;
+import java.sql.SQLXML;
+import java.sql.Savepoint;
+import java.sql.Statement;
+import java.sql.Struct;
+import java.util.ArrayList;
+import java.util.Map;
+import java.util.Properties;
+
+import javax.naming.Context;
+import javax.naming.InitialContext;
+import javax.sql.DataSource;
+import javax.transaction.HeuristicMixedException;
+import javax.transaction.HeuristicRollbackException;
+import javax.transaction.InvalidTransactionException;
+import javax.transaction.NotSupportedException;
+import javax.transaction.RollbackException;
+import javax.transaction.Status;
+import javax.transaction.SystemException;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+import javax.transaction.UserTransaction;
+import javax.transaction.xa.XAResource;
+
+/**
+ * @author <a href="mailto:nfilotto@exoplatform.com">Nicolas
Filotto</a>
+ * @version $Id$
+ *
+ */
+public class TestDataSourceProviderImpl extends BasicTestCase
+{
+ private static String DS_NAME = "TestDataSourceProviderImpl-DS";
+
+ private String oldFactoryName;
+
+ private MyDataSource mds;
+
+ /**
+ * @see junit.framework.TestCase#setUp()
+ */
+ @Override
+ protected void setUp() throws Exception
+ {
+ super.setUp();
+ oldFactoryName = System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
"org.exoplatform.services.naming.SimpleContextFactory");
+ new InitialContext().bind(DS_NAME, mds = new MyDataSource());
+ }
+
+ /**
+ * @see junit.framework.TestCase#tearDown()
+ */
+ @Override
+ protected void tearDown() throws Exception
+ {
+ try
+ {
+ new InitialContext().unbind(DS_NAME);
+ }
+ finally
+ {
+ if (oldFactoryName == null)
+ {
+ System.clearProperty(Context.INITIAL_CONTEXT_FACTORY);
+ }
+ else
+ {
+ System.setProperty(Context.INITIAL_CONTEXT_FACTORY, oldFactoryName);
+ }
+ super.tearDown();
+ }
+ }
+
+ public void testIsManaged() throws Exception
+ {
+ DataSourceProvider dsp = new DataSourceProviderImpl(null);
+ assertFalse(dsp.isManaged(DS_NAME));
+
+ InitParams params = new InitParams();
+ dsp = new DataSourceProviderImpl(params);
+ assertFalse(dsp.isManaged(DS_NAME));
+
+ ValueParam paramConf = new ValueParam();
+ paramConf.setName(DataSourceProviderImpl.PARAM_ALWAYS_MANAGED);
+ paramConf.setValue("true");
+ params.addParameter(paramConf);
+ dsp = new DataSourceProviderImpl(params);
+ assertTrue(dsp.isManaged(DS_NAME));
+
+ paramConf.setValue("false");
+ dsp = new DataSourceProviderImpl(params);
+ assertFalse(dsp.isManaged(DS_NAME));
+
+ ValuesParam paramsConf = new ValuesParam();
+ paramsConf.setName(DataSourceProviderImpl.PARAM_MANAGED_DS);
+ ArrayList<String> values = new ArrayList<String>();
+ values.add(DS_NAME);
+ values.add(" ds-foo1, ds-foo2 ");
+ values.add("ds-foo3");
+ paramsConf.setValues(values);
+ params.addParameter(paramsConf);
+ dsp = new DataSourceProviderImpl(params);
+ assertTrue(dsp.isManaged(DS_NAME));
+ assertTrue(dsp.isManaged("ds-foo1"));
+ assertTrue(dsp.isManaged("ds-foo2"));
+ assertTrue(dsp.isManaged("ds-foo3"));
+ }
+
+ public void testGetDataSource() throws Exception
+ {
+ DataSourceProvider dsp = new DataSourceProviderImpl(null);
+ DataSource ds = dsp.getDataSource(DS_NAME);
+ assertNotNull(ds);
+ Connection con = ds.getConnection();
+ con.commit();
+ assertTrue(mds.con.committed);
+ con = ds.getConnection(null, null);
+ con.commit();
+ assertTrue(mds.con.committed);
+
+ MyTransactionService mts = new MyTransactionService();
+ mts.tm.setStatus(Status.STATUS_ACTIVE);
+ dsp = new DataSourceProviderImpl(null, mts);
+ ds = dsp.getDataSource(DS_NAME);
+ assertNotNull(ds);
+ con = ds.getConnection();
+ con.commit();
+ assertTrue(mds.con.committed);
+ con = ds.getConnection(null, null);
+ con.commit();
+ assertTrue(mds.con.committed);
+ mts.tm.setStatus(Status.STATUS_NO_TRANSACTION);
+ con = ds.getConnection();
+ con.commit();
+ assertTrue(mds.con.committed);
+ con = ds.getConnection(null, null);
+ con.commit();
+ assertTrue(mds.con.committed);
+
+ InitParams params = new InitParams();
+ ValueParam paramConf = new ValueParam();
+ paramConf.setName(DataSourceProviderImpl.PARAM_ALWAYS_MANAGED);
+ paramConf.setValue("true");
+ params.addParameter(paramConf);
+ dsp = new DataSourceProviderImpl(params, mts);
+ ds = dsp.getDataSource(DS_NAME);
+ assertNotNull(ds);
+ mts.tm.setStatus(Status.STATUS_ACTIVE);
+ con = ds.getConnection();
+ con.commit();
+ assertFalse(mds.con.committed);
+ con = ds.getConnection(null, null);
+ con.commit();
+ assertFalse(mds.con.committed);
+ mts.tm.setStatus(Status.STATUS_NO_TRANSACTION);
+ con = ds.getConnection();
+ con.commit();
+ assertTrue(mds.con.committed);
+ con = ds.getConnection(null, null);
+ con.commit();
+ assertTrue(mds.con.committed);
+
+ paramConf = new ValueParam();
+ paramConf.setName(DataSourceProviderImpl.PARAM_CHECK_TX);
+ paramConf.setValue("false");
+ params.addParameter(paramConf);
+ dsp = new DataSourceProviderImpl(params, mts);
+ ds = dsp.getDataSource(DS_NAME);
+ assertNotNull(ds);
+ mts.tm.setStatus(Status.STATUS_ACTIVE);
+ con = ds.getConnection();
+ con.commit();
+ assertFalse(mds.con.committed);
+ con = ds.getConnection(null, null);
+ con.commit();
+ assertFalse(mds.con.committed);
+ mts.tm.setStatus(Status.STATUS_NO_TRANSACTION);
+ con = ds.getConnection();
+ con.commit();
+ assertFalse(mds.con.committed);
+ con = ds.getConnection(null, null);
+ con.commit();
+ assertFalse(mds.con.committed);
+ }
+
+ private static class MyDataSource implements DataSource
+ {
+ public MyConnection con;
+
+ /**
+ * @see javax.sql.CommonDataSource#getLogWriter()
+ */
+ public PrintWriter getLogWriter() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see javax.sql.CommonDataSource#getLoginTimeout()
+ */
+ public int getLoginTimeout() throws SQLException
+ {
+ return 0;
+ }
+
+ /**
+ * @see javax.sql.CommonDataSource#setLogWriter(java.io.PrintWriter)
+ */
+ public void setLogWriter(PrintWriter arg0) throws SQLException
+ {
+ }
+
+ /**
+ * @see javax.sql.CommonDataSource#setLoginTimeout(int)
+ */
+ public void setLoginTimeout(int arg0) throws SQLException
+ {
+ }
+
+ /**
+ * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
+ */
+ public boolean isWrapperFor(Class<?> arg0) throws SQLException
+ {
+ return false;
+ }
+
+ /**
+ * @see java.sql.Wrapper#unwrap(java.lang.Class)
+ */
+ public <T> T unwrap(Class<T> arg0) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see javax.sql.DataSource#getConnection()
+ */
+ public Connection getConnection() throws SQLException
+ {
+ return con = new MyConnection();
+ }
+
+ /**
+ * @see javax.sql.DataSource#getConnection(java.lang.String, java.lang.String)
+ */
+ public Connection getConnection(String username, String password) throws
SQLException
+ {
+ return con = new MyConnection();
+ }
+ }
+
+ private static class MyConnection implements Connection
+ {
+
+ public boolean committed;
+
+ /**
+ * @see java.sql.Wrapper#isWrapperFor(java.lang.Class)
+ */
+ public boolean isWrapperFor(Class<?> iface) throws SQLException
+ {
+ return false;
+ }
+
+ /**
+ * @see java.sql.Wrapper#unwrap(java.lang.Class)
+ */
+ public <T> T unwrap(Class<T> iface) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String)
+ */
+ public PreparedStatement prepareStatement(String sql) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#prepareCall(java.lang.String)
+ */
+ public CallableStatement prepareCall(String sql) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#nativeSQL(java.lang.String)
+ */
+ public String nativeSQL(String sql) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#setAutoCommit(boolean)
+ */
+ public void setAutoCommit(boolean autoCommit) throws SQLException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#commit()
+ */
+ public void commit() throws SQLException
+ {
+ committed = true;
+ }
+
+ /**
+ * @see java.sql.Connection#rollback()
+ */
+ public void rollback() throws SQLException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#close()
+ */
+ public void close() throws SQLException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#setReadOnly(boolean)
+ */
+ public void setReadOnly(boolean readOnly) throws SQLException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#setCatalog(java.lang.String)
+ */
+ public void setCatalog(String catalog) throws SQLException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#setTransactionIsolation(int)
+ */
+ public void setTransactionIsolation(int level) throws SQLException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#clearWarnings()
+ */
+ public void clearWarnings() throws SQLException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#createArrayOf(java.lang.String, java.lang.Object[])
+ */
+ public Array createArrayOf(String arg0, Object[] arg1) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#createBlob()
+ */
+ public Blob createBlob() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#createClob()
+ */
+ public Clob createClob() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#createNClob()
+ */
+ public NClob createNClob() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#createSQLXML()
+ */
+ public SQLXML createSQLXML() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#createStatement()
+ */
+ public Statement createStatement() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#createStatement(int, int)
+ */
+ public Statement createStatement(int resultSetType, int resultSetConcurrency)
throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String, int, int)
+ */
+ public PreparedStatement prepareStatement(String sql, int resultSetType, int
resultSetConcurrency)
+ throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#prepareCall(java.lang.String, int, int)
+ */
+ public CallableStatement prepareCall(String sql, int resultSetType, int
resultSetConcurrency) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#setTypeMap(java.util.Map)
+ */
+ public void setTypeMap(Map<String, Class<?>> map) throws SQLException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#setSavepoint()
+ */
+ public Savepoint setSavepoint() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#setSavepoint(java.lang.String)
+ */
+ public Savepoint setSavepoint(String name) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#rollback(java.sql.Savepoint)
+ */
+ public void rollback(Savepoint savepoint) throws SQLException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#releaseSavepoint(java.sql.Savepoint)
+ */
+ public void releaseSavepoint(Savepoint savepoint) throws SQLException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#createStatement(int, int, int)
+ */
+ public Statement createStatement(int resultSetType, int resultSetConcurrency, int
resultSetHoldability)
+ throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#createStruct(java.lang.String, java.lang.Object[])
+ */
+ public Struct createStruct(String arg0, Object[] arg1) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#getAutoCommit()
+ */
+ public boolean getAutoCommit() throws SQLException
+ {
+ return false;
+ }
+
+ /**
+ * @see java.sql.Connection#isClosed()
+ */
+ public boolean isClosed() throws SQLException
+ {
+ return false;
+ }
+
+ /**
+ * @see java.sql.Connection#isReadOnly()
+ */
+ public boolean isReadOnly() throws SQLException
+ {
+ return false;
+ }
+
+ /**
+ * @see java.sql.Connection#getCatalog()
+ */
+ public String getCatalog() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#getClientInfo()
+ */
+ public Properties getClientInfo() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#getClientInfo(java.lang.String)
+ */
+ public String getClientInfo(String arg0) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#getMetaData()
+ */
+ public DatabaseMetaData getMetaData() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#getTransactionIsolation()
+ */
+ public int getTransactionIsolation() throws SQLException
+ {
+ return 0;
+ }
+
+ /**
+ * @see java.sql.Connection#getWarnings()
+ */
+ public SQLWarning getWarnings() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#getTypeMap()
+ */
+ public Map<String, Class<?>> getTypeMap() throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#getHoldability()
+ */
+ public int getHoldability() throws SQLException
+ {
+ return 0;
+ }
+
+ /**
+ * @see java.sql.Connection#isValid(int)
+ */
+ public boolean isValid(int arg0) throws SQLException
+ {
+ return false;
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String, int, int, int)
+ */
+ public PreparedStatement prepareStatement(String sql, int resultSetType, int
resultSetConcurrency,
+ int resultSetHoldability) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#prepareCall(java.lang.String, int, int, int)
+ */
+ public CallableStatement prepareCall(String sql, int resultSetType, int
resultSetConcurrency,
+ int resultSetHoldability) throws SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String, int)
+ */
+ public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws
SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String, int[])
+ */
+ public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws
SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#prepareStatement(java.lang.String, java.lang.String[])
+ */
+ public PreparedStatement prepareStatement(String sql, String[] columnNames) throws
SQLException
+ {
+ return null;
+ }
+
+ /**
+ * @see java.sql.Connection#setClientInfo(java.util.Properties)
+ */
+ public void setClientInfo(Properties arg0) throws SQLClientInfoException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#setClientInfo(java.lang.String, java.lang.String)
+ */
+ public void setClientInfo(String arg0, String arg1) throws SQLClientInfoException
+ {
+ }
+
+ /**
+ * @see java.sql.Connection#setHoldability(int)
+ */
+ public void setHoldability(int holdability) throws SQLException
+ {
+ }
+ }
+
+ private static class MyTransactionService implements TransactionService
+ {
+ public MyTransactionManager tm = new MyTransactionManager();
+
+ /**
+ * @see
org.exoplatform.services.transaction.TransactionService#getTransactionManager()
+ */
+ public TransactionManager getTransactionManager()
+ {
+ return tm;
+ }
+
+ /**
+ * @see
org.exoplatform.services.transaction.TransactionService#getUserTransaction()
+ */
+ public UserTransaction getUserTransaction()
+ {
+ return null;
+ }
+
+ /**
+ * @see
org.exoplatform.services.transaction.TransactionService#getDefaultTimeout()
+ */
+ public int getDefaultTimeout()
+ {
+ return 0;
+ }
+
+ /**
+ * @see
org.exoplatform.services.transaction.TransactionService#setTransactionTimeout(int)
+ */
+ public void setTransactionTimeout(int seconds) throws SystemException
+ {
+ }
+
+ /**
+ * @see
org.exoplatform.services.transaction.TransactionService#enlistResource(javax.transaction.xa.XAResource)
+ */
+ public boolean enlistResource(XAResource xares) throws RollbackException,
SystemException, IllegalStateException
+ {
+ return false;
+ }
+
+ /**
+ * @see
org.exoplatform.services.transaction.TransactionService#delistResource(javax.transaction.xa.XAResource)
+ */
+ public boolean delistResource(XAResource xares) throws RollbackException,
SystemException, IllegalStateException
+ {
+ return false;
+ }
+ }
+
+ private static class MyTransactionManager implements TransactionManager
+ {
+
+ private int status;
+
+ public void setStatus(int status)
+ {
+ this.status = status;
+ }
+
+ /**
+ * @see javax.transaction.TransactionManager#begin()
+ */
+ public void begin() throws NotSupportedException, SystemException
+ {
+ }
+
+ /**
+ * @see javax.transaction.TransactionManager#commit()
+ */
+ public void commit() throws RollbackException, HeuristicMixedException,
HeuristicRollbackException,
+ SecurityException, IllegalStateException, SystemException
+ {
+ }
+
+ /**
+ * @see javax.transaction.TransactionManager#getStatus()
+ */
+ public int getStatus() throws SystemException
+ {
+ return status;
+ }
+
+ /**
+ * @see javax.transaction.TransactionManager#getTransaction()
+ */
+ public Transaction getTransaction() throws SystemException
+ {
+ return null;
+ }
+
+ /**
+ * @see javax.transaction.TransactionManager#resume(javax.transaction.Transaction)
+ */
+ public void resume(Transaction tobj) throws InvalidTransactionException,
IllegalStateException, SystemException
+ {
+ }
+
+ /**
+ * @see javax.transaction.TransactionManager#rollback()
+ */
+ public void rollback() throws IllegalStateException, SecurityException,
SystemException
+ {
+ }
+
+ /**
+ * @see javax.transaction.TransactionManager#setRollbackOnly()
+ */
+ public void setRollbackOnly() throws IllegalStateException, SystemException
+ {
+ }
+
+ /**
+ * @see javax.transaction.TransactionManager#setTransactionTimeout(int)
+ */
+ public void setTransactionTimeout(int seconds) throws SystemException
+ {
+ }
+
+ /**
+ * @see javax.transaction.TransactionManager#suspend()
+ */
+ public Transaction suspend() throws SystemException
+ {
+ return null;
+ }
+
+ }
+}
Modified:
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/TransactionTest.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/TransactionTest.java 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/TransactionTest.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -21,14 +21,11 @@
import junit.framework.TestCase;
import org.exoplatform.container.StandaloneContainer;
-import org.exoplatform.services.log.ExoLogger;
-import org.exoplatform.services.log.Log;
-import org.objectweb.jotm.Current;
import javax.naming.InitialContext;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
import javax.transaction.UserTransaction;
-import javax.transaction.xa.XAResource;
-import javax.transaction.xa.Xid;
/**
* Created by The eXo Platform SAS .<br/> Prerequisites: default-context-factory
@@ -40,8 +37,6 @@
public class TransactionTest extends TestCase
{
- private static Log log =
ExoLogger.getLogger("exo.kernel.component.common.TransactionTest");
-
private StandaloneContainer container;
private TransactionService ts;
@@ -49,12 +44,11 @@
public void setUp() throws Exception
{
-
StandaloneContainer.setConfigurationPath("src/test/java/conf/standalone/test-configuration.xml");
+
StandaloneContainer.setConfigurationPath("src/test/resources/conf/standalone/test-configuration.xml");
container = StandaloneContainer.getInstance();
ts =
(TransactionService)container.getComponentInstanceOfType(TransactionService.class);
-
}
public void testUserTransactionBeforeResource() throws Exception
@@ -63,63 +57,67 @@
UserTransaction ut = ts.getUserTransaction();
ut.begin();
- Current c = (Current)ut;
- //
System.out.printf(">>>>>>>>>>>"+c.getAllTx()[0]);
- // c.getAllXid();
- //
System.out.printf(">>>>>>>>>>>"+c.getAllXid());
- // fail();
-
- // c.getTransactionManager().
-
XAResourceTestImpl xares = new XAResourceTestImpl(ts);
ts.enlistResource(xares);
xares.setFlag(5);
assertEquals(0, xares.getOldFlag());
+ ts.delistResource(xares);
ut.commit();
assertEquals(5, xares.getFlag());
assertEquals(5, xares.getOldFlag());
-
- ts.delistResource(xares);
-
}
public void testUserTransactionAfterResource() throws Exception
{
XAResourceTestImpl xares = new XAResourceTestImpl(ts);
- ts.enlistResource(xares);
+ try
+ {
+ ts.enlistResource(xares);
+ fail("IllegalStateException is expected since it cannot be enlisted without
an active tx");
+ }
+ catch (IllegalStateException e)
+ {
+ // OK
+ }
assertEquals(0, xares.getFlag());
UserTransaction ut = ts.getUserTransaction();
-
ut.begin();
+ ts.enlistResource(xares);
xares.setFlag(5);
assertEquals(0, xares.getOldFlag());
ut.commit();
assertEquals(5, xares.getFlag());
assertEquals(5, xares.getOldFlag());
- ts.delistResource(xares);
+ try
+ {
+ ts.delistResource(xares);
+ fail("IllegalStateException is expected since it cannot be delisted without
an active tx");
+ }
+ catch (IllegalStateException e)
+ {
+ // OK
+ }
}
public void testUserTransactionRollback() throws Exception
{
-
XAResourceTestImpl xares = new XAResourceTestImpl(ts);
- ts.enlistResource(xares);
-
assertEquals(0, xares.getFlag());
UserTransaction ut = ts.getUserTransaction();
ut.begin();
+ ts.enlistResource(xares);
xares.setFlag(5);
assertEquals(5, xares.getFlag());
+ ts.delistResource(xares);
ut.rollback();
assertEquals(0, xares.getFlag());
assertEquals(0, xares.getOldFlag());
- ts.delistResource(xares);
}
public void testUserTransactionFromJndi() throws Exception
@@ -136,13 +134,10 @@
assertEquals(0, xares.getFlag());
xares.setFlag(5);
- // assertEquals(5, xares.getFlag());
+ ts.delistResource(xares);
ut.commit();
assertEquals(5, xares.getFlag());
assertEquals(5, xares.getOldFlag());
-
- ts.delistResource(xares);
-
}
public void testReuseUT() throws Exception
@@ -153,8 +148,7 @@
UserTransaction ut = (UserTransaction)obj;
ut.begin();
- XAResourceTestImpl xares = new XAResourceTestImpl(ts); //(XAResourceTestImpl)f
- // .createResoure();
+ XAResourceTestImpl xares = new XAResourceTestImpl(ts);
ts.enlistResource(xares);
xares.setFlag(5);
@@ -162,60 +156,52 @@
assertEquals(5, xares.getFlag());
assertEquals(5, xares.getOldFlag());
+ ut.begin();
// In a case of reusing Have to enlist the resource once again!
ts.enlistResource(xares);
-
- ut.begin();
xares.setFlag(2);
+ ts.delistResource(xares);
ut.commit();
assertEquals(2, xares.getFlag());
assertEquals(2, xares.getOldFlag());
-
- ts.delistResource(xares);
-
}
- public void testGenerateXid() throws Exception
- {
- Xid id = ts.createXid();
- log.info("XID ==== " + id);
- assertNotNull(id);
- }
-
public void testSimpleGlobalTransaction() throws Exception
{
- Xid id = ts.createXid();
XAResourceTestImpl xares = new XAResourceTestImpl(ts);
- xares.start(id, XAResource.TMNOFLAGS);
+ UserTransaction ut = ts.getUserTransaction();
+ ut.begin();
+ ts.enlistResource(xares);
assertEquals(0, xares.getFlag());
xares.setFlag(1);
- xares.commit(id, true);
+ ut.commit();
assertEquals(1, xares.getFlag());
assertEquals(1, xares.getOldFlag());
}
public void test2GlobalTransactions() throws Exception
{
- Xid id1 = ts.createXid();
XAResourceTestImpl xares = new XAResourceTestImpl(ts);
- xares.start(id1, XAResource.TMNOFLAGS);
+ TransactionManager tm = ts.getTransactionManager();
+ tm.begin();
+ ts.enlistResource(xares);
assertEquals(0, xares.getFlag());
xares.setFlag(1);
- xares.end(id1, XAResource.TMSUSPEND);
+ Transaction tx = tm.suspend();
assertEquals(1, xares.getFlag());
- Xid id2 = ts.createXid();
- xares.start(id2, XAResource.TMNOFLAGS);
+ tm.begin();
+ ts.enlistResource(xares);
xares.setFlag(2);
// End work
- xares.end(id2, XAResource.TMSUCCESS);
+ tm.commit();
// Resume work with former transaction
- xares.start(id1, XAResource.TMRESUME);
+ tm.resume(tx);
// Commit work recorded when associated with xid2
- xares.commit(id2, true);
+ tm.commit();
assertEquals(2, xares.getFlag());
assertEquals(2, xares.getOldFlag());
}
Modified:
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/XAResourceTestImpl.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/XAResourceTestImpl.java 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/XAResourceTestImpl.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -21,9 +21,7 @@
import org.exoplatform.services.log.ExoLogger;
import org.exoplatform.services.log.Log;
-import javax.transaction.RollbackException;
import javax.transaction.Status;
-import javax.transaction.SystemException;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;
@@ -36,39 +34,24 @@
* @version $Id: $
*/
-public class XAResourceTestImpl implements ExoResource, XAResource
+public class XAResourceTestImpl implements XAResource
{
private static Log log =
ExoLogger.getLogger("exo.kernel.component.common.XAResourceTestImpl");
private int timeout = 5;
- // private transient TransactionService ts;
-
private int oldFlag;
private int flag = oldFlag = 0;
private final TransactionService ts;
- private Object payload;
-
public XAResourceTestImpl(TransactionService ts)
{
this.ts = ts;
}
- // public XAResourceTestImpl(TransactionService ts)
- // throws RollbackException, SystemException {
- // this.ts = ts;
- // ts.enlistResource(this);
- // }
-
- public XAResource getXAResource()
- {
- return this;
- }
-
public void commit(Xid arg0, boolean arg1) throws XAException
{
oldFlag = flag;
@@ -134,45 +117,8 @@
this.flag = flag;
}
- public void enlistResource() throws XAException
- {
- try
- {
- ts.enlistResource(this);
- }
- catch (IllegalStateException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- catch (RollbackException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- catch (SystemException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
-
- public void delistResource() throws XAException
- {
- }
-
public int getOldFlag()
{
return oldFlag;
}
-
- public Object getPayload()
- {
- return payload;
- }
-
- public void setPayload(Object payload)
- {
- this.payload = payload;
- }
}
Modified:
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/jcr/TestXASessionIntegration.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/jcr/TestXASessionIntegration.java 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/jcr/TestXASessionIntegration.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -1,57 +0,0 @@
-/*
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.exoplatform.services.transaction.jcr;
-
-import junit.framework.TestCase;
-
-import org.exoplatform.services.transaction.TransactionService;
-import org.exoplatform.services.transaction.impl.jotm.TransactionServiceJotmImpl;
-
-/**
- * Integration test between the behavior of an XASession implementing the ExoResource
interface
- * and the JOTM implementation.
- *
- * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
- * @version $Revision$
- */
-public class TestXASessionIntegration extends TestCase
-{
-
- /** . */
- private TransactionService txservice;
-
- @Override
- protected void setUp() throws Exception
- {
- txservice = new TransactionServiceJotmImpl(null, null);
- }
-
- public void testLoginLogout() throws Exception
- {
- XASession session = new XASession(txservice);
- txservice.enlistResource(session);
- txservice.delistResource(session);
- }
-
- public void testLogout() throws Exception
- {
- XASession session = new XASession(txservice);
- txservice.delistResource(session);
- }
-}
Modified:
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/jcr/XASession.java
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/jcr/XASession.java 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/test/java/org/exoplatform/services/transaction/jcr/XASession.java 2011-05-23
06:57:15 UTC (rev 4401)
@@ -1,187 +0,0 @@
-/*
- * Copyright (C) 2009 eXo Platform SAS.
- *
- * This is free software; you can redistribute it and/or modify it
- * under the terms of the GNU Lesser General Public License as
- * published by the Free Software Foundation; either version 2.1 of
- * the License, or (at your option) any later version.
- *
- * This software is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with this software; if not, write to the Free
- * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
- * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
- */
-package org.exoplatform.services.transaction.jcr;
-
-import org.exoplatform.services.transaction.ExoResource;
-import org.exoplatform.services.transaction.TransactionService;
-
-import javax.transaction.HeuristicMixedException;
-import javax.transaction.HeuristicRollbackException;
-import javax.transaction.RollbackException;
-import javax.transaction.SystemException;
-import javax.transaction.xa.XAException;
-import javax.transaction.xa.XAResource;
-import javax.transaction.xa.Xid;
-
-/**
- * Simulate an xa resource.
- *
- * @author <a href="mailto:julien.viet@exoplatform.com">Julien
Viet</a>
- * @version $Revision$
- */
-public class XASession implements XAResource, ExoResource
-{
-
- /** Transaction service. */
- private final TransactionService tService;
-
- /** Start flags. */
- private int startFlags;
-
- /** . */
- private Object payload;
-
- /** . */
- private int txTimeout;
-
- public XASession(TransactionService tService)
- {
- this.tService = tService;
- this.txTimeout = tService.getDefaultTimeout();
- this.payload = null;
- this.startFlags = TMNOFLAGS;
- }
-
- // ExoResource implementation
-
- public XAResource getXAResource()
- {
- return this;
- }
-
- public void enlistResource() throws XAException
- {
- try
- {
- tService.enlistResource(this);
- }
- catch (RollbackException e)
- {
- throw new XAException(e.getMessage());
- }
- catch (SystemException e)
- {
- throw new XAException(e.getMessage());
- }
- }
-
- public void delistResource() throws XAException
- {
- try
- {
- tService.delistResource(this);
- }
- catch (RollbackException e)
- {
- throw new XAException(e.getMessage());
- }
- catch (SystemException e)
- {
- throw new XAException(e.getMessage());
- }
- }
-
- public Object getPayload()
- {
- return payload;
- }
-
- public void setPayload(Object payload)
- {
- this.payload = payload;
- }
-
- // XAResource implementation
-
- public void commit(Xid xid, boolean b) throws XAException
- {
- try
- {
- tService.getTransactionManager().commit();
- }
- catch (RollbackException e)
- {
- throw new XAException(XAException.XA_RBOTHER);
- }
- catch (HeuristicRollbackException e)
- {
- throw new XAException(XAException.XA_RBOTHER);
- }
- catch (HeuristicMixedException e)
- {
- throw new XAException(XAException.XA_RBOTHER);
- }
- catch (SystemException e)
- {
- throw new XAException(XAException.XA_RBOTHER);
- }
- }
-
- public void end(Xid xid, int flags) throws XAException
- {
- startFlags = flags;
- }
-
- public void forget(Xid xid) throws XAException
- {
- }
-
- public int getTransactionTimeout() throws XAException
- {
- return txTimeout;
- }
-
- public boolean isSameRM(XAResource resource) throws XAException
- {
- return resource == this;
- }
-
- public int prepare(Xid xid) throws XAException
- {
- return XA_OK;
- }
-
- public Xid[] recover(int i) throws XAException
- {
- return null;
- }
-
- public void rollback(Xid xid) throws XAException
- {
- }
-
- public void start(Xid xid, int flags) throws XAException
- {
- startFlags = flags;
- }
-
- public boolean setTransactionTimeout(int seconds) throws XAException
- {
- try
- {
- tService.setTransactionTimeout(seconds);
- }
- catch (SystemException e)
- {
- throw new XAException(e.getMessage());
- }
- this.txTimeout = seconds;
- return true;
- }
-}
Modified:
kernel/trunk/exo.kernel.component.common/src/test/resources/conf/standalone/test-configuration.xml
===================================================================
---
kernel/trunk/exo.kernel.component.common/src/test/resources/conf/standalone/test-configuration.xml 2011-05-20
14:38:19 UTC (rev 4400)
+++
kernel/trunk/exo.kernel.component.common/src/test/resources/conf/standalone/test-configuration.xml 2011-05-23
06:57:15 UTC (rev 4401)
@@ -21,67 +21,7 @@
-->
<configuration
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd
http://www.exoplaform.org/xml/ns/kernel_1_0.xsd"
xmlns="http://www.exoplaform.org/xml/ns/kernel_1_0.xsd">
- <!--
- <component>
- <key>org.exoplatform.services.log.LogConfigurationInitializer</key>
- <type>org.exoplatform.services.log.LogConfigurationInitializer</type>
- <init-params>
- <value-param>
- <name>configurator</name>
-
<value>org.exoplatform.services.log.impl.Log4JConfigurator</value>
- </value-param>
- <properties-param>
- <name>properties</name>
- <description>Log4J properties</description>
- <property name="log4j.rootLogger" value="INFO, stdout,
file" />
- <property name="log4j.appender.stdout"
value="org.apache.log4j.ConsoleAppender" />
-
- <property name="log4j.appender.stdout.layout"
value="org.apache.log4j.PatternLayout" />
- <property name="log4j.appender.stdout.layout.ConversionPattern"
value="%d{dd.MM.yyyy HH:mm:ss} *%-5p* [%t] %c{1}: %m (%F, line %L) %n" />
- <property name="log4j.appender.stdout.threshold"
value="INFO" />
-
- <property name="log4j.appender.file"
value="org.apache.log4j.FileAppender" />
- <property name="log4j.appender.file.File"
value="target/jcr.log" />
-
- <property name="log4j.appender.file.layout"
value="org.apache.log4j.PatternLayout" />
- <property name="log4j.appender.file.layout.ConversionPattern"
value="%d{dd.MM.yyyy HH:mm:ss} *%-5p* [%t] %c{1}: %m (%F, line %L) %n" />
- </properties-param>
-
- <!-- value-param>
- <name>logger</name>
-
<value>org.exoplatform.services.log.impl.BufferedSimpleLog</value>
- </value-param>
- <value-param>
- <name>configurator</name>
-
<value>org.exoplatform.services.log.impl.SimpleLogConfigurator</value>
- </value-param>
- <properties-param>
- <name>properties</name>
- <description>SimpleLog properties</description>
- <property name="org.apache.commons.logging.simplelog.defaultlog"
value="debug" />
- <property
name="org.apache.commons.logging.simplelog.showdatetime" value="true"
/>
- </properties-param -->
-
- <!-- value-param>
- <name>logger</name>
-
<value>org.exoplatform.services.log.impl.BufferedJdk14Logger</value>
- </value-param>
- <value-param>
- <name>configurator</name>
-
<value>org.exoplatform.services.log.impl.Jdk14Configurator</value>
- </value-param>
- <properties-param>
- <name>properties</name>
- <description>jdk1.4 Logger properties</description>
- <property name="handlers"
value="java.util.logging.ConsoleHandler" />
- <property name=".level" value="FINE" />
- <property name="java.util.logging.ConsoleHandler.level"
value="FINE" />
- </properties-param>
-
- </init-params>
- </component -->
-
<component>
<key>org.exoplatform.services.naming.InitialContextInitializer</key>
<type>org.exoplatform.services.naming.InitialContextInitializer</type>
@@ -146,12 +86,6 @@
<description>Default initial context properties</description>
<property name="java.naming.factory.initial"
value="org.exoplatform.services.naming.SimpleContextFactory" />
</properties-param>
- <!-- properties-param>
- <name>mandatory-properties</name>
- <description>Mandatory initial context properties</description>
- <property name="java.naming.factory.initial"
value="org.exoplatform.services.naming.SimpleContextFactory" />
- <property name="java.naming.provider.url"
value="rmi://localhost:9999" />
- </properties-param -->
</init-params>
</component>
@@ -163,10 +97,6 @@
<name>timeout</name>
<value>5</value>
</value-param>
- <!-- value-param>
- <name>track-without-transaction</name>
- <value>true</value>
- </value-param -->
</init-params>
</component>