我遇到了Spring回滾事務的問題。我有一種方法可以創建插入多個表格的新訂單(users
,order
,order_product
...)。如果在方法結束之前有異常,則回滾啓動並刪除order_product
中的記錄,但users
記錄仍然存在。我想刪除在我的數據庫中生成的所有記錄。 users id
字段(主鍵)由MySQL 5.6中的Autoincrement生成。而order_product
的主鍵是兩個外鍵。當「執行標識立即插入」時,Spring不會回滾
用戶表:
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) DEFAULT NULL,
`email` varchar(255) NOT NULL,
`new_email` varchar(255) DEFAULT NULL,
`allowed_newsletter` bit(1) NOT NULL DEFAULT b'1',
`lastname` varchar(255) DEFAULT NULL,
`phone` varchar(255) DEFAULT NULL,
`lang` enum('en','de','fr','es') DEFAULT 'es',
`creation_date` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `IDX_EMAIL` (`email`) USING BTREE
) ENGINE=InnoDB AUTO_INCREMENT=1092 DEFAULT CHARSET=latin1;
order_product表:
CREATE TABLE `order_product` (
`product_id` int(11) NOT NULL,
`order_id` int(11) NOT NULL,
`detail_json` longtext,
`shipping_date` datetime DEFAULT NULL,
`order_product_status` enum('PaymentPending','ShippingPending','Sent','Cancelled') NOT NULL DEFAULT 'PaymentPending',
`is_downloaded` bit(1) NOT NULL DEFAULT b'0',
PRIMARY KEY (`product_id`,`order_id`),
KEY `IDX_OP_PRODUCT_ID` (`product_id`) USING BTREE,
KEY `IDX_OP_ORDER_ID` (`order_id`) USING BTREE,
CONSTRAINT `FK_OP_ORDER_ID` FOREIGN KEY (`order_id`) REFERENCES `order_customer` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT `FK_OP_PRODUCT_ID` FOREIGN KEY (`product_id`) REFERENCES `product` (`id`) ON DELETE NO ACTION ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8
我使用MySQL 5.6我的Hibernate相關的4和Spring 4
日誌文件:
[annotation.AnnotationTransactionAttributeSource(getTransactionAttribute:108)] Adding transactional method 'UsersDAOImpl.insertUser' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT;
[jpa.JpaTransactionManager(doGetTransaction:334)] Found thread-bound EntityManager [[email protected]] for JPA transaction
[jpa.JpaTransactionManager(handleExistingTransaction:476)] Participating in existing transaction
[spi.ActionQueue(addResolvedEntityInsertAction:213)] Executing identity-insert immediately
[hibernate.SQL(logStatement:104)]
insert
into
users
(allowed_newsletter, email, lang, lastname, name, new_email, phone)
values
(?, ?, ?, ?, ?, ?, ?)
2016-12-19 09:47:46,046 DEBUG (http-bio-8080-exec-3) [id.IdentifierGeneratorHelper(getGeneratedIdentity:93)] Natively generated identity: 1091
2016-12-19 09:47:46,047 DEBUG (http-bio-8080-exec-3) [spi.ActionQueue(addResolvedEntityInsertAction:213)] Executing identity-insert immediately
.....
... MORE INSERTs - SELECTs - etc ...
.....
[annotation.AnnotationTransactionAttributeSource(getTransactionAttribute:108)] Adding transactional method 'OrderProductServiceImpl.addNewOrderProduct' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
[jpa.JpaTransactionManager(doGetTransaction:334)] Found thread-bound EntityManager [[email protected]] for JPA transaction
[jpa.JpaTransactionManager(handleExistingTransaction:476)] Participating in existing transaction
[annotation.AnnotationTransactionAttributeSource(getTransactionAttribute:108)] Adding transactional method 'OrderProductDAOImpl.addNewOrderProduct' with attribute: PROPAGATION_REQUIRED,ISOLATION_DEFAULT; ''
[jpa.JpaTransactionManager(doGetTransaction:334)] Found thread-bound EntityManager [[email protected]] for JPA transaction
[jpa.JpaTransactionManager(handleExistingTransaction:476)] Participating in existing transaction
[internal.AbstractSaveEventListener(saveWithGeneratedId:130)] Generated identifier: component[orderId,productId]{orderId=144, productId=2553}, using strategy: org.hibernate.id.CompositeNestedGeneratedValueGenerator
[writers.HstsHeaderWriter(writeHeaders:130)] Not injecting HSTS header since it did not match the requestMatcher org.springframework.se[email protected]1d011fbb
[context.HttpSessionSecurityContextRepository(saveContext:352)] SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
[internal.SessionImpl(disconnect:566)] Disconnecting session
[internal.LogicalConnectionImpl(releaseConnection:232)] Releasing JDBC connection
[internal.LogicalConnectionImpl(releaseConnection:250)] Released JDBC connection
[jpa.JpaTransactionManager(processRollback:851)] Initiating transaction rollback
[jpa.JpaTransactionManager(doRollback:538)] Rolling back JPA transaction on EntityManager [[email protected]]
[spi.AbstractTransactionImpl(rollback:203)] rolling back
[jdbc.JdbcTransaction(doRollback:164)] rolled JDBC Connection
[jdbc.JdbcTransaction(releaseManagedConnection:126)] re-enabling autocommit
[internal.JdbcCoordinatorImpl(close:173)] HHH000420: Closing un-released batch
[jpa.JpaTransactionManager(doCleanupAfterCompletion:600)] Closing JPA EntityManager [[email protected]] after transaction
[jpa.EntityManagerFactoryUtils(closeEntityManager:435)] Closing JPA EntityManager
[internal.LogicalConnectionImpl(releaseConnection:232)] Releasing JDBC connection
[internal.LogicalConnectionImpl(releaseConnection:250)] Released JDBC connection
編輯:
@Controller
public class OrderController {
@ResponseStatus(value = HttpStatus.ACCEPTED)
@Transactional
public void finishPayment(...) {
//Call to service methods
}
}
@Service
@Transactional
public class UsersServiceImpl implements UsersService {
public UsersEntity registerNewGuest(String username, String email, MCountryEntity countryEntity, String nickname, String lang) {
// Insert User
}
}
@Service
@Transactional
public class OrderProductServiceImpl implements OrderProductService {
@Override
public void addNewOrderProduct(OrderProductEntity orderProductEntity) {
// Insert OrderProduct
}
}
請也張貼相關的代碼。看起來你有一個'@ Transactional'用於每個數據庫插入/更新。在這種情況下,你瞄準什麼,不會工作 –
我添加了Java代碼。 – Miguel