In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
Today, the editor will share with you the relevant knowledge points about the methods commonly used by activiti transactions and business transactions. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
A database transaction usually contains a sequence of read / write operations to the database. It exists for the following two purposes:
It provides a method for the database operation sequence to recover from failure to the normal state, and provides a way for the database to maintain consistency even in the abnormal state.
When multiple applications are accessing the database concurrently, you can provide an isolation method between these applications to prevent each other's operations from interfering with each other.
It is believed that each project has its own transaction control management method. But how do you use it in conjunction with activiti transactions?
There are a lot of data on activiti spring-based transaction integration network. 1000 words are omitted here.
However, some projects do not use spring, so how to control the transaction?
1. Create the configuration information for activiti:
StandaloneProcessEngineConfiguration conf = (StandaloneProcessEngineConfiguration) ProcessEngineConfiguration .createStandaloneProcessEngineConfiguration ()
2. Set activiti configuration information (such as whether to update automatically, whether to use history, font, etc.):
Conf.setDatabaseSchemaUpdate ("true"); conf.setDbHistoryUsed (true); conf.setHistory ("full"); conf.setActivityFontName ("Song style"); conf.setJobExecutorActivate (false)
3. Set the DataSource of the database
Conf.setDataSource (DBManager.getDataSource ())
Please note that this DBManager.getDataSource () is self-encapsulated code.
4. Set up a transaction management factory (I wrote this method CustomJdbcTransactionFactory myself, which will be described in detail below):
CustomJdbcTransactionFactory jdbcTransactionFactory= new CustomJdbcTransactionFactory (); conf.setTransactionFactory (jdbcTransactionFactory)
That's the point.
Your own transaction will open a database connection Connection conn = dataSource.getConnection (), and all your operations will be completed in this connection. When activiti manipulates data, it also opens a connection dataSource.getConnection () to manipulate its own data. Then there will be problems. How can we talk about business if we are not in the same connection?
There is a variable transaction manager in StandaloneProcessEngineConfiguration.
We can override the method of opening the connection in the transaction manager and then in the set configuration object conf.
Import java.sql.Connection
Import java.sql.SQLException;import javax.sql.DataSource;import org.apache.ibatis.logging.Log;import org.apache.ibatis.logging.LogFactory;import org.apache.ibatis.session.TransactionIsolationLevel;import org.apache.ibatis.transaction.jdbc.JdbcTransaction;import com.fangdo.core.db.DBManager;public class CustomJdbcTransaction extends JdbcTransaction {private static final Log log = LogFactory.getLog (CustomJdbcTransaction.class); public CustomJdbcTransaction (Connection connection) {super (connection) } public CustomJdbcTransaction (DataSource ds, TransactionIsolationLevel desiredLevel, boolean desiredAutoCommit) {super (ds, desiredLevel, desiredAutoCommit);} @ Override protected void openConnection () throws SQLException {/ / super.openConnection (); connection = DBManager.getConnection () If (log.isDebugEnabled ()) {log.debug ("{CustomJdbcTransaction} Openning JDBC Connection" + connection.hashCode () + "[]" + autoCommmit);} / / connection = dataSource.getConnection (); if (level! = null) {connection.setTransactionIsolation (level.getLevel ());} setDesiredAutoCommit (autoCommmit) @ Override public void close () throws SQLException {if (log.isDebugEnabled ()) {log.debug ("{CustomJdbcTransaction} closing JDBC Connection" + connection.hashCode ());} / / super.close () @ Override public void commit () throws SQLException {/ / TODO Auto-generated method stub// super.commit ();} @ Override public void rollback () throws SQLException {/ / TODO Auto-generated method stub// super.rollback ();}}
The openConnection () method is overridden to get the connection to the database that my business opened.
The connection closes close (), commits commit (), rolls back rollback (), and logs out. All the operations of the database connection are controlled by my business, which is not controlled by activiti.
Import java.sql.Connection;import java.util.Properties;import javax.sql.DataSource;import org.apache.ibatis.session.TransactionIsolationLevel;import org.apache.ibatis.transaction.Transaction;import org.apache.ibatis.transaction.jdbc.JdbcTransaction;import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;public class CustomJdbcTransactionFactory extends JdbcTransactionFactory {@ Override public void setProperties (Properties props) {super.setProperties (props) } @ Override public Transaction newTransaction (Connection conn) {/ / return super.newTransaction (conn); return new CustomJdbcTransaction (conn);} @ Override public Transaction newTransaction (DataSource ds, TransactionIsolationLevel level, boolean autoCommit) {/ / return super.newTransaction (ds, level, autoCommit) Return new CustomJdbcTransaction (ds, level, autoCommit);}}
CustomJdbcTransactionFactory is to create a factory. It's easy to understand. 5. Create an engine:
ProcessEngine = conf.buildProcessEngine ()
6. Use examples:
QueryHelper.startTransaction (); / / Open transaction taskService.claim (taskId, getPhoneId ()); taskService.complete (taskId, variables); / / your own business code QueryHelper.endTransaction (); / / close the transaction
Your business and activiti share the same transaction. If an exception is thrown, it will be rolled back.
Here's a brief description of what QueryHelper.startTransaction () mainly does:
Connection conn = dataSource.getConnection (); conn.setAutoCommit (false)
The main things QueryHelper.endTransaction () do are:
Connection connection = getConnection (); connection.commit (); / / commit JDBC transaction connection.setAutoCommit (true); / / restore the default commit mode of JDBC transaction. These are all the contents of the article "methods used by activiti transactions and business transactions". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 255
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.