mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
format code
This commit is contained in:
@@ -16,6 +16,8 @@ import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import com.alibaba.druid.filter.FilterAdapter;
|
||||
import com.alibaba.druid.filter.FilterChain;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.spring.IUserService;
|
||||
import com.alibaba.druid.spring.User;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
|
||||
public class SpringFilterTest extends TestCase {
|
||||
@@ -35,17 +37,37 @@ public class SpringFilterTest extends TestCase {
|
||||
"com/alibaba/druid/pool/spring-config-1.xml");
|
||||
|
||||
DataSource dataSource = (DataSource) context.getBean("dataSource");
|
||||
Connection conn = dataSource.getConnection();
|
||||
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.execute("select 1");
|
||||
stmt.execute("select 2");
|
||||
stmt.execute("select 1");
|
||||
stmt.close();
|
||||
conn.close();
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.execute("CREATE TABLE sequence_seed (value INTEGER, name VARCHAR(50))");
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.execute("CREATE TABLE t_User (id BIGINT, name VARCHAR(50))");
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
{
|
||||
Connection conn = dataSource.getConnection();
|
||||
Statement stmt = conn.createStatement();
|
||||
stmt.execute("insert into sequence_seed (value ,name) values (0, 'druid-spring-test')");
|
||||
stmt.close();
|
||||
conn.close();
|
||||
}
|
||||
|
||||
// user-service
|
||||
IUserService service = (IUserService) context.getBean("user-service");
|
||||
User user = new User();
|
||||
user.setName("xx");
|
||||
service.addUser(user);
|
||||
|
||||
TestFilter filter = (TestFilter) context.getBean("test-filter");
|
||||
Assert.assertEquals(1, filter.getConnectCount());
|
||||
Assert.assertEquals(2, filter.getConnectCount());
|
||||
|
||||
context.close();
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.alibaba.druid.spring;
|
||||
|
||||
public interface ISequenceDao {
|
||||
|
||||
boolean compareAndSet(String name, int value, int expect);
|
||||
|
||||
int getValue(String name);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.alibaba.druid.spring;
|
||||
|
||||
|
||||
public interface ISequenceService {
|
||||
long nextValue();
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.alibaba.druid.spring;
|
||||
|
||||
|
||||
public interface IUserDao {
|
||||
void addUser(User user);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.alibaba.druid.spring;
|
||||
|
||||
|
||||
public interface IUserService {
|
||||
void addUser(User user);
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.alibaba.druid.spring;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
|
||||
|
||||
public class SequenceDao extends SqlMapClientDaoSupport implements ISequenceDao {
|
||||
|
||||
public boolean compareAndSet(String name, int value, int expect) {
|
||||
|
||||
Map<String, Object> parameters = new HashMap<String, Object>();
|
||||
parameters.put("name", name);
|
||||
parameters.put("value", value);
|
||||
parameters.put("expect", expect);
|
||||
|
||||
int updateCount = getSqlMapClientTemplate().update("Sequence.compareAndSet", parameters);
|
||||
|
||||
return updateCount == 1;
|
||||
}
|
||||
|
||||
public int getValue(String name) {
|
||||
return (Integer) getSqlMapClientTemplate().queryForObject("Sequence.getValue", name);
|
||||
}
|
||||
|
||||
public int getValueForUpdate(String name) {
|
||||
return (Integer) getSqlMapClientTemplate().queryForObject("Sequence.getValueForUpdate", name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.alibaba.druid.spring;
|
||||
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
public class SequenceService implements SequenceServiceMBean, ISequenceService {
|
||||
|
||||
private ISequenceDao dao;
|
||||
private String name = "druid-spring-test";
|
||||
|
||||
private String seed;
|
||||
|
||||
private AtomicInteger increment = new AtomicInteger();
|
||||
|
||||
public ISequenceDao getDao() {
|
||||
return dao;
|
||||
}
|
||||
|
||||
public void setDao(ISequenceDao dao) {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getSeed() {
|
||||
return this.seed;
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public long nextValue() {
|
||||
if (seed == null) {
|
||||
init();
|
||||
}
|
||||
|
||||
int value = increment.getAndIncrement();
|
||||
|
||||
String text = seed + "9" + Integer.toString(value);
|
||||
|
||||
return Long.parseLong(text);
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public synchronized void init() {
|
||||
this.seed = nextSeed();
|
||||
}
|
||||
|
||||
@Transactional(propagation = Propagation.NOT_SUPPORTED)
|
||||
public synchronized String nextSeed() {
|
||||
for (;;) {
|
||||
int value = dao.getValue(name);
|
||||
if (dao.compareAndSet(name, value + 1, value)) {
|
||||
if (value == 0) {
|
||||
return "";
|
||||
} else {
|
||||
return Integer.toString(value, 9);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package com.alibaba.druid.spring;
|
||||
|
||||
|
||||
public interface SequenceServiceMBean {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package com.alibaba.druid.spring;
|
||||
|
||||
public class User {
|
||||
|
||||
private long id;
|
||||
private String name;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.alibaba.druid.spring;
|
||||
|
||||
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
|
||||
|
||||
public class UserDao extends SqlMapClientDaoSupport implements IUserDao {
|
||||
|
||||
public void addUser(User user) {
|
||||
getSqlMapClientTemplate().insert("User.insert", user);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.alibaba.druid.spring;
|
||||
|
||||
import org.springframework.transaction.annotation.Propagation;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
public class UserService implements IUserService {
|
||||
|
||||
private IUserDao dao;
|
||||
private ISequenceService sequenceService;
|
||||
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public void addUser(User user) {
|
||||
user.setId(sequenceService.nextValue());
|
||||
dao.addUser(user);
|
||||
}
|
||||
|
||||
public IUserDao getDao() {
|
||||
return dao;
|
||||
}
|
||||
|
||||
public void setDao(IUserDao dao) {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
public ISequenceService getSequenceService() {
|
||||
return sequenceService;
|
||||
}
|
||||
|
||||
public void setSequenceService(ISequenceService sequenceService) {
|
||||
this.sequenceService = sequenceService;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user