我有以下代码,我在这里设置@Transactional(readOnly = true)
。@Transactional不能与readOnly = true
主要方法中的代码。
ApplicationContext context = Utils.getContext();
AnnotatedCrudDao service = new AnnotatedCrudDao();
DataSource dataSource = (DataSource) context.getBean("mySqlDataSource");
service.setDataSource(dataSource);
service.insert(account, user, movie);
@Transactional
public class AnnotatedCrudDao extends JdbcDaoSupport {
private static Logger logger;
static {
logger = Logger.getLogger(AnnotatedCrudDao.class);
}
@Transactional(readOnly = true)
public void insert(Account account, User user, MovieTicket movie) {
TicketUtils.insertAccount(getJdbcTemplate(), account);
TicketUtils.insertUser(getJdbcTemplate(), user);
TicketUtils.insertMovie(getJdbcTemplate(), movie);
}
}
class TicketUtils{
public static void insertUser(JdbcTemplate template, User user) {
String queryUser = "INSERT INTO t_user_txn (ID, NAME, ACCOUNT_ID, TICKETID) VALUES (?,?,?,?)";
logger.debug("queryUser" + queryUser);
template.update(queryUser, new Object[] { user.getId(), user.getName(),
user.getAccount().getId(), user.getTicketId() });
}
public static void insertMovie(JdbcTemplate template, MovieTicket movie) {
String queryMovie = "INSERT INTO t_movieticket_txn (ID, MOVIENAME, TOTALTICKETSCOUNT, PRICE) VALUES (?,?,?,?)";
logger.debug("queryMovie:" + queryMovie);
template.update(queryMovie, new Object[] { movie.getId(),
movie.getMovieName(), movie.getTotalTicketsCount(),
movie.getPrice() });
}
public static void insertAccount(JdbcTemplate template, Account account) {
String queryAccount = "INSERT INTO t_account_txn (ID, AMOUNT) VALUES (?,?)";
logger.debug("queryAccount:" + queryAccount);
template.update(queryAccount, new Object[] { account.getId(),
account.getAmount() });
}
}
方面
<bean id="mySqlDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:qadb7"/>
<property name="username" value="tp2"/>
<property name="password" value="tp2"/>
</bean>
<bean id="dsTxnMgr" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="mySqlDataSource" />
</bean>
<!-- Add this tag to enable annotations transactions -->
<tx:annotation-driven transaction-manager="dsTxnMgr" />
甚至认为我已经设置为@Transactional(readOnly = true)
插入方法,还是正在执行插入操作。不应该将设置为true的属性readOnly
注意不能在此方法上执行插入操作。
我加了@Repository。它仍然不起作用。 –
确保添加包扫描以将bean包含到应用程序上下文中。 –