Main.java 的代码:
public class Main {
public static void main(String[] args) {
final ApplicationContext ctx = new ClassPathXmlApplicationContext("spring.xml");
final UserManager userManager = (UserManager) ctx.getBean("userManagerImpl");
new Thread() {
public void run() {
User user = new User();
user.setUsername("hari18");
user.setName("haris1");
userManager.insertUser(user);
System.out.println("User inserted!");
}
} .start();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
new Thread() {
public void run() {
List<User> users = userManager.getUsers();
System.out.println("\nUser list fetched!" + "\nUser count: " + users.size());
for (User user1 : users) {
System.out.println(user1.getUsername());
}
}
} .start();
}
}
UserManagerImpl.java 的代码:
@Service
@Scope("prototype")
public class UserManagerImpl implements UserManager {
@Autowired
private UserDAO userDAO;
@Override
@Transactional( propagation=Propagation.REQUIRED , isolation=Isolation.SERIALIZABLE)
public void insertUser(User user) {
userDAO.insertUser(user);
}
@Override
@Transactional( propagation=Propagation.REQUIRED , isolation=Isolation.SERIALIZABLE)
public List<User> getUsers() {
return userDAO.getUsers();
}
}
UserDAOImpl.java 的代码:
@Service
public class UserDAOImpl extends JdbcDaoSupport implements UserDAO {
@Autowired
public UserDAOImpl(DataSource dataSource) {
setDataSource(dataSource);
}
@Override
public void insertUser(User user) {
getJdbcTemplate().update("INSERT INTO USER (USERNAME, NAME) VALUES (?, ?)",
new Object[] {
user.getUsername(),
user.getName()
}
);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public List<User> getUsers() {
List<User> users = getJdbcTemplate().query("SELECT * FROM USER",new UserMapper());
return users;
}
}
spring.xml 的代码:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<tx:annotation-driven transaction-manager="transactionManager"/>
<context:component-scan base-package="com.byteslounge.spring" />
<!-- context:component-scan base-package="com.byteslounge.spring" /-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="root" />
<property name="password" value="password" />
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
当运行上面的主类时,理论上线程一将首先启动并且它必须锁定表USER
(因为isolation = Isolation.SERIALIZABLE
)并且当线程二运行时它必须等到从表中释放锁USER
而不从表中读取表,但是当我运行时此代码正在读取表格并打印,而无需等待第一笔交易完成。为什么USER
即使隔离是SERIALIZABLE
???表锁也不起作用?