1

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???表锁也不起作用?

4

2 回答 2

1

我得到了这个问题的答案。这里的问题是线程一和拖曳之间的时间延迟,这里我给了时间延迟 100ms 但是这个延迟是不够的,因为在 insertUser() 获取表锁之前 getUsers() 占用锁并获取数据并处理它,所以一次我将时间延迟更改为 1000 毫秒,此问题已解决。所以 insertUser() 占用了锁, getUsers() 等到 insertUser() 完成并提交插入部分。

于 2013-09-11T14:18:33.650 回答
1

我认为解决方案是..

在读取的情况下,锁将在多线程(切碎模式)之间共享。因此“选择”查询正在正确执行。如果您尝试使用更新/删除查询,您将得到您所想的场景(另一个线程必须等到第一个线程完成)。以这种方式尝试。

于 2014-01-29T08:19:57.220 回答