Announcement Announcement Module
No announcement yet.
TransactionManager Problem with xml and Code Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • TransactionManager Problem with xml and Code

    Hi I have start using Spring since yesterday. I have tried to code a little example.
    I get it working but I have a problem if I try to read the database settings litke host, user, password from xml.
    If I integrate it directly to my code it works.

    Here is my Code I hope someone can help me:
    Model Class
    public class User {
    	private Integer _uid;
    	private String _username;
    	private String _password;
    	public User(Integer uid, String username, String password){
    	public User(){
    ....Some getters and Setters here
    My Interface:
    public interface UserDao {
    	int insertUser(User record);
    	int updateUser(User record);
    	void deleteUser(int key);
    	User selectUser(int key);
    Here the implementation of the interface:
    public class UserDaoImpl implements UserDao{
    	private DataSourceTransactionManager transactionManager;
    	private JdbcTemplate _jdbcTemplate;
    	public UserDaoImpl(){
    		DataSource dataSource = transactionManager.getDataSource();
    // IF I use that config it works;)		
    //	  MysqlDataSource dataSource = new MysqlDataSource();
    //        dataSource.setUser("demo");
    //        dataSource.setPassword("demo");
    //        dataSource.setServerName("myserver";
    //        dataSource.setPort(3306);
    //        dataSource.setDatabaseName("demo");
    		_jdbcTemplate = new JdbcTemplate(dataSource);
    	public void setTransactionManager(DataSourceTransactionManager transactionManager){
    		this.transactionManager = transactionManager;
    	public int insertUser(User record) {
    	public int updateUser(User record) {
    	public void deleteUser(int key) {
    	public User selectUser(int key) {
    		return user;

    Thats my Test Class where I start:
    public class UserDaoTest extends TestCase{
    	private static final Integer UID = new Integer(1);
    	private static final String USRID = "username";
    	private static final String PSWD = "password";
    	protected static ApplicationContext _appContext;
    	private static UserDao _userDao;
    	private static User _record;
    	public UserDaoTest(String arg){
    	public static Test suite(){
    		TestSuite suite = new TestSuite();
    		suite.addTest(new UserDaoTest("InsertTest"));
    		suite.addTest(new UserDaoTest("UpdateTest"));
    		suite.addTest(new UserDaoTest("DeleteTest"));
    		return suite;
    	public void setUp() throws Exception {
    		_userDao = (UserDao) getBean("userDao");
    		_record = new User();
    	public static void main(String[] args) {;
    	public synchronized static void init()
    		if (_appContext == null){
    			String path = System.getProperty("user.dir");
    			String slash = System.getProperty("file.separator");
    			String configDir = path + slash + "config" +slash;
    			DOMConfigurator.configure(configDir+ "log4j.xml");
    			_appContext = new FileSystemXmlApplicationContext(new String[] {configDir + "appContext.xml"});
    			_userDao = (UserDao) _appContext.getBean("userDao");
    	protected static Object getBean(String beanName){
    		return _appContext.getBean(beanName);
    	public void InsertTest(){
    	public void UpdateTest(){
    	public void DeleteTest(){

    And at last my AppContext.xml
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""
    	<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp.BasicDataSource">
            	<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
            	<property name="url" value="jdbc:mysql://myserver:3306/demo"/>
           	 	<property name="username" value="demo"/>
            	<property name="password" value="demo"/>
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     	 	<property name="dataSource" ref="dataSource"/>
    	<bean id="userDao" class="">
    		<property name="transactionManager" ref="transactionManager"/>
    The problem is that the transactionManager is null and the connection throw mysql didn't work so i get a null result and that error will be shown:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDao' defined in file [C:[my-dir]\config\appContext.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class ..............
    Thanks for your help


  • #2
    For starters I suggest a read on java.. You expect a dependency to be already injected whilst you invoke the constructor, however for setter injection to work you already need an instance of an object...

    But why on earth are you injecting the DataSourceTransactionMaager getting the datasource from that?! Why... Simply use declarative transactions as explained in the reference guide, next to that I suggest using springs test support instead of inventing your own. Finally I suggest using JdbcTemplate as that makes your jdbc code a lot cleaner.