Results 1 to 4 of 4

Thread: Cannot insert data

  1. #1

    Default Cannot insert data

    Nothing I have tried succeeds in persisting data to the database. No exceptions are thrown, but the data is not written to the database.
    Database is MySQL version 5.1.53-community MySQL Community Server. Oddly enough when I output the result of the the (SimpleJdbcInsert) execute() method, it says 1 row updated. I have verified no rows were updated by running a query directly from the DB command line.

    bean definitions:
    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-3.0.xsd">
    	   		
    	<bean id="projectPropertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    			<property name="location" value="classpath:project.properties"/>
    	</bean>
    	
    	<bean id="DBManager" class="com.invoicer.dataaccess.InvoicerDBManager">
    		<property name="dataSource" ref="dataSource"/>
    	</bean>
    	
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="username" value="${username}"/>
    		<property name="password" value="${password}"/>
    		<property name="url" value="jdbc:mysql://localhost:3306/${database}"/>
    		<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    		<property name="initialSize" value="5"/>
    		<property name="maxActive" value="5"/>
    		<property name="defaultAutoCommit" value="false"/>
    	</bean>
    	
    </beans>
    DAO Class:
    Code:
    public class InvoicerDBManager implements DBManager
    {
    	private SimpleJdbcTemplate template;
    	private SimpleJdbcInsert insert;
    	private DataSource dataSource;
    
    	private static InvoicerDBManager ISNTANCE;
    
    	private InvoicerDBManager()
    	{
    		// SINGLETON
    	}
    
    	public static InvoicerDBManager getInstance()
    	{
    		if (ISNTANCE == null)
    		{
    			ISNTANCE = new InvoicerDBManager();
    		}
    
    		return ISNTANCE;
    	}
    
    	@Override
    	public void setDataSource(DataSource dataSource)
    	{
    		template = new SimpleJdbcTemplate(dataSource);
    		this.dataSource = dataSource;
    	}
    
    	@Override
    	public void update(String table, Map<String, Object> columnMaps)
    	{
    		insert = new SimpleJdbcInsert(dataSource).withTableName(table);
    		System.out.println(insert.execute(columnMaps)); //--> this prints out 1
    	}
    
            @Override
    	public <T> T queryForObject(String query, Map<String, ?> binds, Class<T> classRef)
    	{
    		return template.queryForObject(query, classRef, binds);
    	}
    }
    Insert:

    Code:
    	
    public class InvoiceTest extends TestRunner
    {
    	private Invoice testInvoice;
    
    	@Before
    	public void init() throws Exception
    	{
    		testInvoice = new Invoice();
    	}
    
           @Test
    	public void foo()
    	{
    		InvoicerDBManager db = InvoicerDBManager.getInstance();
    		Map<String, Object> map = new HashMap<String, Object>();
    		map.put("a", "b");
    		try
    		{
    		     db.update("foo", map);
    		}
    		catch(DataAccessException x)
    		{
    			System.out.println("****");
    			System.out.println(x.toString());
    		}
    		
    		Map<String, Object> binds = new HashMap<String, Object>();
    		binds.put("val", "b");	
    		String query="SELECT * from  foo where a =:val";
    		assertEquals("b",db.queryForObject(query, binds, String.class));
    	}
    Parent class that inits datasource:
    Code:
    import javax.sql.DataSource;
    
    import org.junit.runner.RunWith;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.test.context.ContextConfiguration;
    import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
    
    import com.invoicer.dataaccess.InvoicerDBManager;
    
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = { "classpath:dao.xml" }) 
    public class TestRunner
    {
    	private static final InvoicerDBManager dbManager = InvoicerDBManager.getInstance();
    
    	@Autowired
    	public void setDataSource(DataSource dataSource)
    	{
    		dbManager.setDataSource(dataSource);
    	}
    Console output:
    Code:
    Jan 2, 2011 11:46:33 PM org.springframework.test.context.TestContextManager retrieveTestExecutionListeners
    INFO: @TestExecutionListeners is not present for class [class com.invoicer.invoice.InvoiceTest]: using defaults.
    Jan 2, 2011 11:46:33 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [dao.xml]
    Jan 2, 2011 11:46:34 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
    INFO: Refreshing org.springframework.context.support.GenericApplicationContext@596e1fb1: startup date [Sun Jan 02 23:46:34 EST 2011]; root of context hierarchy
    Jan 2, 2011 11:46:34 PM org.springframework.core.io.support.PropertiesLoaderSupport loadProperties
    INFO: Loading properties file from class path resource [project.properties]
    Jan 2, 2011 11:46:34 PM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1f8166e5: defining beans [projectPropertyConfigurer,DBManager,dataSource,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor]; root of factory hierarchy
    1
    Last edited by publicstaticvoidmain; Jan 2nd, 2011 at 10:49 PM.

  2. #2

    Default Never Mind

    Never mind, i had defaultAutoCommit in the dataSource definition set to false. Fail.

  3. #3
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,630

    Default

    NEVER use autocommit you really don't want that. Use proper tx management and configuration...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  4. #4

    Default

    Thanks. yes I know not to use it. I haven't implemented transaction support yet. Just trying to get spring DAO up and running first.

    Cheers

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •