简体   繁体   English

Spring JDBC事务管理器

[英]Spring JDBC transaction manager

I try to write a transaction manager using JDBC in Spring. 我尝试在Spring中使用JDBC编写事务管理器。

my app-servlet.xml 我的app-servlet.xml

<!-- JDBC Config -->
<bean   id="dataSource" 
        class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close" 
        p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" 
        p:username="${jdbc.username}" 
        p:password="${jdbc.password}" />

<!-- dataSource TransactionManager -->
<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>

<bean id="UserDAOImpl" class="com.project.dao.impl.UserDAOImpl">
    <property name="transactionManager" ref="transactionManager"/>
</bean>

my UserDAOImpl.java 我的UserDAOImpl.java

public class UserDAOImpl implements UserDAO {

//transaction manager
private DataSourceTransactionManager transactionManager;
private JdbcTemplate jdbcTemplate;

public UserDAOImpl() {
    super();
    DataSource dataSource = transactionManager.getDataSource();
    jdbcTemplate = new JdbcTemplate(dataSource);
}

public void setTransactionManager(DataSourceTransactionManager transactionManager)    {
    this.transactionManager = transactionManager;
}
....
}

Even though I have transactionManager Bean in my app-servlet, UserDAOImpl won't be instatiated because transactionManager is null . 即使我的app-servlet中有transactionManager Bean, UserDAOImpl也不会被实例化,因为transactionManagernull Probably I miss some point but couldn't find out what is wrong. 可能我会错过一些观点,但无法找出问题所在。

You should use constructor injection for your transaction manager. 您应该为事务管理器使用构造函数注入。 Spring is going to call the constructor before it can inject the transactionManager property. Spring将在注入transactionManager属性之前调用构造函数。

 public UserDAOImpl() 
 {
    /* Transaction Manager NOT set yet */
    DataSource dataSource = transactionManager.getDataSource();
 }

Change it to use constructor injection 将其更改为使用构造函数注入

 public UserDAOImpl(TransactionManager transactionManager) ...

Then configuration 然后配置

 <bean id="UserDAOImpl" class="com.project.dao.impl.UserDAOImpl">
   <constructor-arg ref="transactionManager"/>
 </bean>

If you are using spring with annotations, this should work 如果你使用注释弹簧,这应该工作

@Repository
public class UserDAOImpl implements UserDAO{

@Autowired
private DataSourceTransactionManager transactionManager;

private JdbcTemplate jdbcTemplate;

@Autowired
public void setDataSource(DataSource dataSource) {
    this.jdbcTemplate = new JdbcTemplate(dataSource);
}

and you can do away with this line in the configuration 你可以在配置中取消这一行

<bean id="UserDAOImpl" class="com.project.dao.impl.UserDAOImpl">
<property name="transactionManager" ref="transactionManager"/>
</bean>

instead do a component scan: 而是进行组件扫描:

<!-- Scans within the base package of the application for @Components to configure as beans -->
<context:component-scan base-package="${your package}" />

You can refer more on this in the documentation . 您可以在文档中详细介绍 If you aren't using annotations change the release version in the URL of the given link to the one you are using. 如果您未使用注释,请将给定链接的URL中的发行版更改为您正在使用的链接。 That has enough enough examples to do the same without annotations. 这有足够的例子来做同样的没有注释。

Do you have a properties file or other ways in which these variables are being initilaized 您是否有属性文件或其他方式来初始化这些变量

p:driverClassName="${jdbc.driverClassName}"
        p:url="${jdbc.databaseurl}" 
        p:username="${jdbc.username}" 
        p:password="${jdbc.password}"

These are place-holders and will need values, for example in this link 这些是占位符,需要值,例如在此链接中

There is a properties file called jdbc.properties that is defining the data for driver-class name etc. 有一个名为jdbc.properties的属性文件,用于定义驱动程序类名称等的数据。

Here is the sample source code for the example 下面是样本的源代码示例

You could also modify obtaining jdbcTemplate: 您还可以修改获取jdbcTemplate:

public void setTransactionManager(DataSourceTransactionManager transactionManager)    {
  this.transactionManager = transactionManager;
  DataSource dataSource = transactionManager.getDataSource();
  jdbcTemplate = new JdbcTemplate(dataSource);
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM