您好我在这里收到超时异常,因为标题声明并且我没有找到任何相关答案,因为它是唯一发生的事务。此外,当我拿走交易状态时,我的测试通过就好了。我的创作方法:
@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();
}
我的实际测试:
@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);
}
我的数据源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>
我是新手,所以如果我可以添加任何东西,请说,我已经摔跤了一段时间,所以任何提示都值得赞赏。编辑1:这是我正在运行的唯一测试,即createAMember
答案 0 :(得分:0)
似乎我要么提出了一个模糊或困难的问题,要么我以错误的方式问过它。无论如何,经过多次拔毛,我找到了这个问题的答案。我在这里尝试CreateAMember
的原始代码:
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();
必须更整齐地构建,同时识别类型并将其整齐地放在准备好的声明中。下面的重新跳汰对我有用。
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);
我原来的方法适用于纯粹的JDBC交互,这就是混乱的起因。交易需要如上所示的结构。我希望这篇文章可以帮助一个无知的人,在某些时候也像我一样。