Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:mvc="http://www.springframework.org/schema/mvc"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.2.xsd">
- <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
- <context:component-scan base-package="someproject" />
- <!-- <context:annotation-config /> -->
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/somedb" />
- <property name="username" value="xxx" />
- <property name="password" value="yyy" />
- </bean>
- <bean id="transactionManager"
- class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource"></property>
- </bean>
- @Service
- public class BusinessManagerImpl implements BusinessManager{
- @Autowired
- private UserDao userDao;
- @Autowired
- private ProductDao productDao;
- ....
- @Override
- @Transactional(propagation=Propagation.REQUIRED)
- public User addUser(User user) throws Exception {
- // TODO Auto-generated method stub
- User tempUser = userDao.addUser(user);
- productDao.getAllProducts();
- return tempUser;
- }
- @Service
- public class UserDaoImpl implements UserDao {
- private DataSource dataSource;
- @Autowired
- public UserDaoImpl(DataSource dataSource) {
- super();
- setDataSource(dataSource);
- }
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
- private JdbcTemplate getJdbcTemplate(){
- JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
- return jdbcTemplate;
- }
- ...
- @Override
- @Transactional(propagation=Propagation.MANDATORY)
- public User addUser(final User user) {
- KeyHolder holder = new GeneratedKeyHolder();
- final String sql = "insert into user (username, password) "
- + " VALUES (?, ?)";
- getJdbcTemplate().update(new PreparedStatementCreator() {
- @Override
- public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
- PreparedStatement ps = connection.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
- int index = 1;
- ps.setString(index++, user.getUsername());
- ps.setString(index++, user.getPassword());
- return ps;
- }
- }, holder);
- int seq = holder.getKey().intValue();
- user.setSeq(seq);
- return user;
- }
- @Service
- public class ProductDaoImpl implements ProductDao {
- private DataSource dataSource;
- @Autowired
- public ProductDaoImpl(DataSource dataSource) {
- super();
- setDataSource(dataSource);
- }
- public void setDataSource(DataSource dataSource) {
- this.dataSource = dataSource;
- }
- @Override
- @Transactional(propagation=Propagation.MANDATORY)
- public List<Product> getAllProducts() throws Exception {
- if(true)
- throw new RuntimeException("on purpose");
- JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
- List<Product> products = jdbcTemplate.query(
- "select * from product",
- new ProductRowMapper());
- return products;
- }
- @RestController
- public class RestUserController {
- private static Logger logger = LoggerFactory.getLogger(RestUserController.class);
- @Autowired
- private BusinessManager businessManager;
- @RequestMapping(value = "/adduser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
- public ResponseEntity<String> createEmployee(@RequestBody User user)
- {
- logger.debug("adding user:"+user);
- User addedUser=null;
- try {
- addedUser = businessManager.addUser(user);
- return new ResponseEntity(addedUser, HttpStatus.CREATED);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement