Today in the SPRING3+MYBATIC3 project, use transaction management data to insert. But in exceptional cases do not roll back,
We found a meeting. The data source that was configured when you configured the transaction manager Datasourcetransactionmanager is the data source for another library.
<bean id= "Transactionmanagerlte" class= "Org.springframework.jdbc.datasource.DataSourceTransactionManager" > <property name= "DataSource" ref= "Datasourcelte"/></bean>
Defaulttransactiondefinition def = new Defaulttransactiondefinition ();d Ef.setpropagationbehavior ( transactiondefinition.propagation_required); Transactionstatus status = Txmanager.gettransaction (Def); try {lteteledao.insertltetfjprodserv (DTOs); Lteteledao.inserttfjgroup (tfjgroupdto); Txmanager.commit (status);} catch (Exception e) { txmanager.rollback (status); }
The above is the background, Introduction point spring Transaction
1, when you use spring programming for transaction management, the general steps
- Configuring Datasourcetransactionmananger in the Spring configuration file (above)
- Getting an IOC container Platformtransactionmanager instance in code (step one bean)
- Starts a transaction by Platformtransactionmanager.gettransaction (), returning the Transactionstatus object
- In the need of a consistency processing database, Try...catch.
- Normal process, Platformtransaction.commit (transactionstatus), when an exception occurs, call Platformtransaction.rollback (Transactionstatus)
How the 2,spring Platformtransactionmanager is associated with the underlying data source
In the opening section, using a JDBC data source, the transaction is also a JDBC transaction. In the configuration file, only the Datasourcetransactionmanager (transaction manager) and the Basicdatasource (database connection pool) are associated.
The true JDBC transaction must be managed with a JDBC connection.
Case:
try{ transactionstatus ts = platformtransaction.gettransaction (..); Connection conn = Datasource.getconnection (); Conn Insert .... Platformtransaction.commint (TS);
}catch (Exception e) { platformtransaction.rollback (TS);}
In this case, the business is not connection, why?
This is because Datasource.getconnection () gets the database connection with Platformtransactionmnanger when the transaction is started, the associated database connection is not the same connection.
Not a channel, how can control it.
You can use spring-provided datasourceutils.getconnection (DataSource) to return and Platformtransactionmanager associated connection.
When Platformtransaction initiates a transaction, the connection to the current transaction is registered to the Transactionsynchronizationmanager. Datasourceutils will also first attempt to get the connection that has been associated to the current transaction from the Transactionsynchronizationmanager.
Transaction management in spring multiple data sources