Hi I am getting a timeout exception here as the title states and I am not finding any relevant answers as it is the only transaction occurring. Also when I take away the transaction status my test passes just fine. My method for creation:
@Transactional(readOnly = false, propagation = Propagation.REQUIRES_NEW)
public void createAMember(int memberid, String name, String address1, String address2, String town, String contact_number, int bookallowance, double balance, boolean active) {
String SQL = "insert into member (id,name,address1,address2,town,contact_number,book_allowance, balance, active) values (?,?,?,?,?,?,?, ?, ?)";
getJdbcTemplate().update(SQL, new Object[] { memberid,name,address1,address2,town,contact_number,bookallowance,balance,active});
System.out.println("Created Member Name = " + name + " memberid= " + memberid);
//throw new libException();
}
My actual test:
@Test
@DatabaseSetup(value="classpath:dbEntries.xml", type=DatabaseOperation.CLEAN_INSERT)
public void testCreateMember() throws SQLException{
MemberDaoJDBCTemplate memberDaoJDBCTemplate=(MemberDaoJDBCTemplate)autoWireContext.getBean("memberDaoJDBCTemplate");
//int firstCount=memberDaoJDBCTemplate.countAllMembers();
memberDaoJDBCTemplate.createAMember(id,Name, Address1,Address2,Town,Contact_number,book_allowance,Balance,active);
System.out.println("kafka");
//int secondCount=memberDaoJDBCTemplate.countAllMembers();
//assertNotEquals(firstCount, secondCount);
}
My datasource bean:
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/library"/>
<property name="username" value="root"/></bean>
I am pretty new to this so if I can add anything please just say, I have been wrestling this for awhile so any hints appreciated.Edit1: it is the only test I am running ie createAMember
It seems that I have either asked a obscure or difficult question or I have asked it in the wrong manner of place. At any rate after much hair pulling I found the answer to this question. My original code that actually attempts to CreateAMember
here:
public void createAMember(int memberid, String name, String address1, String address2, String town, String contact_number, int bookallowance, double balance, boolean active) { String SQL = "insert into member (id,name,address1,address2,town,contact_number,book_allowance, balance, active) values (?,?,?,?,?,?,?, ?, ?)"; getJdbcTemplate().update(SQL, new Object[] { memberid,name,address1,address2,town,contact_number,bookallowance,balance,active}); System.out.println("Created Member Name = " + name + " memberid= " + memberid); //throw new libException();
Must be structured more neatly and while identifying types and putting it neatly within a prepared statement. The below re-jigging worked for me. PreparedStatementCreatorFactory psc=new PreparedStatementCreatorFactory(SQL); psc.addParameter(new SqlParameter("id", Types.INTEGER)); psc.addParameter(new SqlParameter("name", Types.VARCHAR)); psc.addParameter(new SqlParameter("address1", Types.VARCHAR)); psc.addParameter(new SqlParameter("address2", Types.VARCHAR)); psc.addParameter(new SqlParameter("contact_number", Types.VARCHAR)); psc.addParameter(new SqlParameter("book_allowance", Types.INTEGER)); psc.addParameter(new SqlParameter("balance", Types.DOUBLE)); psc.addParameter(new SqlParameter("active", Types.BOOLEAN)); Object[] params=new Object[]{memberid,name, address1, address2, town, contact_number, bookallowance, balance,active}; KeyHolder holder = new GeneratedKeyHolder(); getJdbcTemplate().update(psc.newPreparedStatementCreator(params), holder); System.out.println("holder:"+holder.getKey().toString()); System.out.println("Created Record Name = " + name + " ID = " + memberid);
My original method was fine for plain JDBC interaction and this is where the confusion arose. Transactions need the structure as shown above. I hope this post may assist an ignorant so and so like myself at some point.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.