Spring TransactionTemplate demo!!!

    技术2022-05-19  28

     Spring JdbcTemplate 用法总结!!!这篇文章的补充!!!

    前面那篇文章做了这么多,就是没有真实的运行过程序,这里运行了下,居然不知道怎么拿连接拉。

    到处找了一下才找到了,下面贴出代码:

     

    DBUtils

    import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import org.apache.log4j.Logger; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.jdbc.core.simple.SimpleJdbcTemplate; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.support.TransactionTemplate; public class DBUtils { private static Logger logger = Logger.getLogger(DBUtils.class); // private static String oracleDS = "java:OracleDS"; private static DataSource dataSource = null; static { try { Context context = new InitialContext(); dataSource = (DataSource) context.lookup("MySqlDS"); } catch (NamingException e) { logger.info("查找数据源失败···", e); } } public static TransactionTemplate getTransactionTemplate() { PlatformTransactionManager txManager = new DataSourceTransactionManager( dataSource); TransactionTemplate txTemplate = new TransactionTemplate(txManager); return txTemplate; } public static JdbcTemplate getJdbcTemplate(TransactionTemplate txTemplate) { DataSourceTransactionManager txManager = (DataSourceTransactionManager) txTemplate .getTransactionManager(); DataSource dataSource = txManager.getDataSource(); JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); return jdbcTemplate; } public static JdbcTemplate getJdbcTemplate() { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource); return jdbcTemplate; } public static NamedParameterJdbcTemplate getNamedParameterJdbcTemplate() { NamedParameterJdbcTemplate jdbcTemplate = new NamedParameterJdbcTemplate( dataSource); return jdbcTemplate; } public static SimpleJdbcTemplate getSimpleJdbcTemplate() { SimpleJdbcTemplate jdbcTemplate = new SimpleJdbcTemplate(dataSource); return jdbcTemplate; } }

     

    Test

    import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.transaction.TransactionStatus; import org.springframework.transaction.support.TransactionCallbackWithoutResult; import org.springframework.transaction.support.TransactionTemplate; @SuppressWarnings("all") public class Test { public static void main(String[] args) { final TransactionTemplate txTemplate = DBUtils.getTransactionTemplate(); txTemplate.execute(new TransactionCallbackWithoutResult() { protected void doInTransactionWithoutResult(TransactionStatus status) { try { JdbcTemplate jdbcTemplate = DBUtils .getJdbcTemplate(txTemplate); String sql1 = "insert into test(id,name) values(1,'张三')"; String sql2 = "insert into test(id,name) values(2,'李四')"; jdbcTemplate.execute(sql1); if (false) { throw new Exception("故意的!!!"); } jdbcTemplate.execute(sql2); } catch (Exception e) { status.setRollbackOnly(); e.printStackTrace(); } } }); } }


    最新回复(0)