Announcement Announcement Module
Collapse
No announcement yet.
TransactionManager Problem with xml and Code Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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 User.java
    Code:
    public class User {
    
    	private Integer _uid;
    	private String _username;
    	private String _password;
    	
    	public User(Integer uid, String username, String password){
    		this.set_uid(uid);
    		this.set_username(username);
    		this.set_password(password);
    	}
    	
    	public User(){
    	}
    ....Some getters and Setters here
    My Interface:
    Code:
    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:
    Code:
    public class UserDaoImpl implements UserDao{
    	private DataSourceTransactionManager transactionManager;
    	private JdbcTemplate _jdbcTemplate;
    	
    	public UserDaoImpl(){
    		super();
    		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:
    Code:
    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){
    		super(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 {
    		init();
    		_userDao = (UserDao) getBean("userDao");
    		_record = new User();
    		_record.set_uid(UID);
    		_record.set_username(USRID);
    		_record.set_password(PSWD);
    	}
    	
    	public static void main(String[] args) {
    		junit.textui.TestRunner.run(suite());
    	}
    	
    	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
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <beans xmlns="http://www.springframework.org/schema/beans"
    	   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
     
    	<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>	
    
    	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
     	 	<property name="dataSource" ref="dataSource"/>
    	</bean>
    
    	<bean id="userDao" class="mytest.services.UserDaoImpl">
    		<property name="transactionManager" ref="transactionManager"/>
    	</bean>
    
    </beans>
    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:
    Code:
    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

    Regards
    Crazymodder

  • #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.

    Comment

    Working...
    X