改版EntityXInfo
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -29,101 +29,154 @@ final class DataJPASource implements DataSource {
|
||||
|
||||
private final Logger logger = Logger.getLogger(DataJPASource.class.getSimpleName());
|
||||
|
||||
@Override
|
||||
public <T> void updateColumnIncrement(Class<T> clazz, Serializable id, String column, long incvalue) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void updateColumnIncrement(DataConnection conn, Class<T> clazz, Serializable id, String column, long incvalue) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> void refreshCache(Class<T> clazz) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T find(Class<T> clazz, FilterBean bean) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number getCountDistinctSingleResult(Class entityClass, String column) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number getCountDistinctSingleResult(Class entityClass, String column, FilterBean bean) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, V> Sheet<V> queryColumnSheet(String selectedColumn, Class<T> clazz, Flipper flipper, FilterBean bean) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, V> Set<V> queryColumnSet(String selectedColumn, Class<T> clazz, String column, Serializable key) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, V> Set<V> queryColumnSet(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T, V> List<V> queryColumnList(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> int[] queryColumnIntSet(String selectedColumn, Class<T> clazz, String column, Serializable key) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> long[] queryColumnLongSet(String selectedColumn, Class<T> clazz, String column, Serializable key) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> int[] queryColumnIntList(String selectedColumn, Class<T> clazz, String column, Serializable key) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> long[] queryColumnLongList(String selectedColumn, Class<T> clazz, String column, Serializable key) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> int[] queryColumnIntSet(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> long[] queryColumnLongSet(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> int[] queryColumnIntList(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> long[] queryColumnLongList(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T> T find(Class<T> clazz, SelectColumn selects, Serializable pk) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T> void delete(Class<T> clazz, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T> void delete(DataConnection conn, Class<T> clazz, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public Number getMaxSingleResult(Class entityClass, String column, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public Number getMinSingleResult(Class entityClass, String column, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public Number getSumSingleResult(Class entityClass, String column, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public Number getCountSingleResult(Class entityClass, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public Number getCountDistinctSingleResult(Class entityClass, String column, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public Number getAvgSingleResult(Class entityClass, String column, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T> T find(Class<T> clazz, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T> List<T> queryList(Class<T> clazz, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T> List<T> queryList(Class<T> clazz, SelectColumn selects, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T> Sheet<T> querySheet(Class<T> clazz, Flipper flipper, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T> Sheet<T> querySheet(Class<T> clazz, SelectColumn selects, Flipper flipper, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T, V> Set<V> queryColumnSet(String selectedColumn, Class<T> clazz, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T, V> Set<V> queryColumnSet(String selectedColumn, Class<T> clazz, FilterBean bean) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T, V> List<V> queryColumnList(String selectedColumn, Class<T> clazz, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T, V> List<V> queryColumnList(String selectedColumn, Class<T> clazz, FilterBean bean) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
public <T, V> Sheet<V> queryColumnSheet(String selectedColumn, Class<T> clazz, Flipper flipper, FilterNode node) {
|
||||
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
|
||||
}
|
||||
|
||||
private static class DataJPAConnection extends DataConnection {
|
||||
|
||||
private final EntityManager manager;
|
||||
@@ -133,12 +186,10 @@ final class DataJPASource implements DataSource {
|
||||
this.manager = m;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
manager.close();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean commit() {
|
||||
try {
|
||||
manager.getTransaction().commit();
|
||||
@@ -148,7 +199,6 @@ final class DataJPASource implements DataSource {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void rollback() {
|
||||
manager.getTransaction().rollback();
|
||||
}
|
||||
@@ -176,12 +226,10 @@ final class DataJPASource implements DataSource {
|
||||
if (flag) logger.setLevel(Level.FINEST);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataConnection createReadConnection() {
|
||||
return new DataJPAConnection(factory.createEntityManager());
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataConnection createWriteConnection() {
|
||||
return new DataJPAConnection(factory.createEntityManager());
|
||||
}
|
||||
@@ -197,7 +245,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param <T>
|
||||
* @param values
|
||||
*/
|
||||
@Override
|
||||
public <T> void insert(T... values) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -218,7 +265,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param conn
|
||||
* @param values
|
||||
*/
|
||||
@Override
|
||||
public <T> void insert(final DataConnection conn, T... values) {
|
||||
final EntityManager manager = conn.getConnection();
|
||||
for (T value : values) {
|
||||
@@ -233,7 +279,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param <T>
|
||||
* @param values
|
||||
*/
|
||||
@Override
|
||||
public <T> void delete(T... values) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -252,7 +297,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param conn
|
||||
* @param values
|
||||
*/
|
||||
@Override
|
||||
public <T> void delete(final DataConnection conn, T... values) {
|
||||
final EntityManager manager = conn.getConnection();
|
||||
delete(manager, values);
|
||||
@@ -271,7 +315,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param clazz
|
||||
* @param ids 主键值
|
||||
*/
|
||||
@Override
|
||||
public <T> void delete(Class<T> clazz, Serializable... ids) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -291,7 +334,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param clazz
|
||||
* @param ids
|
||||
*/
|
||||
@Override
|
||||
public <T> void delete(final DataConnection conn, Class<T> clazz, Serializable... ids) {
|
||||
final EntityManager manager = conn.getConnection();
|
||||
delete(manager, clazz, ids);
|
||||
@@ -312,7 +354,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param column
|
||||
* @param keys
|
||||
*/
|
||||
@Override
|
||||
public <T> void deleteByColumn(Class<T> clazz, String column, Serializable... keys) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -333,7 +374,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param column
|
||||
* @param keys
|
||||
*/
|
||||
@Override
|
||||
public <T> void deleteByColumn(final DataConnection conn, Class<T> clazz, String column, Serializable... keys) {
|
||||
final EntityManager manager = conn.getConnection();
|
||||
deleteByColumn(manager, clazz, column, keys);
|
||||
@@ -356,7 +396,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param column2
|
||||
* @param key2
|
||||
*/
|
||||
@Override
|
||||
public <T> void deleteByTwoColumn(Class<T> clazz, String column1, Serializable key1, String column2, Serializable key2) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -379,7 +418,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param column2
|
||||
* @param key2
|
||||
*/
|
||||
@Override
|
||||
public <T> void deleteByTwoColumn(final DataConnection conn, Class<T> clazz, String column1, Serializable key1, String column2, Serializable key2) {
|
||||
deleteByTwoColumn((EntityManager) conn.getConnection(), clazz, column1, key1, column2, key2);
|
||||
}
|
||||
@@ -447,7 +485,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param <T>
|
||||
* @param values
|
||||
*/
|
||||
@Override
|
||||
public <T> void update(T... values) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -466,7 +503,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param conn
|
||||
* @param values
|
||||
*/
|
||||
@Override
|
||||
public <T> void update(final DataConnection conn, T... values) {
|
||||
final EntityManager manager = conn.getConnection();
|
||||
update(manager, values);
|
||||
@@ -487,7 +523,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param column
|
||||
* @param value
|
||||
*/
|
||||
@Override
|
||||
public <T> void updateColumn(Class<T> clazz, Serializable id, String column, Serializable value) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -509,7 +544,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param column
|
||||
* @param value
|
||||
*/
|
||||
@Override
|
||||
public <T> void updateColumn(final DataConnection conn, Class<T> clazz, Serializable id, String column, Serializable value) {
|
||||
final EntityManager manager = conn.getConnection();
|
||||
updateColumn(manager, clazz, id, column, value);
|
||||
@@ -530,7 +564,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param value
|
||||
* @param columns
|
||||
*/
|
||||
@Override
|
||||
public <T> void updateColumns(final T value, final String... columns) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -550,7 +583,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param value
|
||||
* @param columns
|
||||
*/
|
||||
@Override
|
||||
public <T> void updateColumns(final DataConnection conn, final T value, final String... columns) {
|
||||
final EntityManager manager = conn.getConnection();
|
||||
updateColumns(manager, value, columns);
|
||||
@@ -571,56 +603,46 @@ final class DataJPASource implements DataSource {
|
||||
|
||||
//-----------------------getSingleResult-----------------------------
|
||||
//-----------------------------MAX-----------------------------
|
||||
@Override
|
||||
public Number getMaxSingleResult(final Class entityClass, final String column) {
|
||||
return getMaxSingleResult(entityClass, column, null);
|
||||
return getMaxSingleResult(entityClass, column, (FilterBean) null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number getMaxSingleResult(final Class entityClass, final String column, FilterBean bean) {
|
||||
return getSingleResult(ReckonType.MAX, entityClass, column, bean);
|
||||
}
|
||||
|
||||
//-----------------------------MIN-----------------------------
|
||||
@Override
|
||||
public Number getMinSingleResult(final Class entityClass, final String column) {
|
||||
return getMinSingleResult(entityClass, column, null);
|
||||
return getMinSingleResult(entityClass, column, (FilterBean) null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number getMinSingleResult(final Class entityClass, final String column, FilterBean bean) {
|
||||
return getSingleResult(ReckonType.MIN, entityClass, column, bean);
|
||||
}
|
||||
|
||||
//-----------------------------SUM-----------------------------
|
||||
@Override
|
||||
public Number getSumSingleResult(final Class entityClass, final String column) {
|
||||
return getSumSingleResult(entityClass, column, null);
|
||||
return getSumSingleResult(entityClass, column, (FilterBean) null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number getSumSingleResult(final Class entityClass, final String column, FilterBean bean) {
|
||||
return getSingleResult(ReckonType.SUM, entityClass, column, bean);
|
||||
}
|
||||
|
||||
//----------------------------COUNT----------------------------
|
||||
@Override
|
||||
public Number getCountSingleResult(final Class entityClass) {
|
||||
return getCountSingleResult(entityClass, null);
|
||||
return getCountSingleResult(entityClass, (FilterBean) null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number getCountSingleResult(final Class entityClass, FilterBean bean) {
|
||||
return getSingleResult(ReckonType.COUNT, entityClass, null, bean);
|
||||
}
|
||||
|
||||
//-----------------------------AVG-----------------------------
|
||||
@Override
|
||||
public Number getAvgSingleResult(final Class entityClass, final String column) {
|
||||
return getAvgSingleResult(entityClass, column, null);
|
||||
return getAvgSingleResult(entityClass, column, (FilterBean) null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Number getAvgSingleResult(final Class entityClass, final String column, FilterBean bean) {
|
||||
return getSingleResult(ReckonType.AVG, entityClass, column, bean);
|
||||
}
|
||||
@@ -673,7 +695,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param pk
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> T find(Class<T> clazz, Serializable pk) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -691,7 +712,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> T[] find(Class<T> clazz, Serializable... ids) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -715,7 +735,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> T findByColumn(Class<T> clazz, String column, Serializable key) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -740,7 +759,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param key2
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> T findByTwoColumn(Class<T> clazz, String column1, Serializable key1, String column2, Serializable key2) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -789,7 +807,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param keys
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> T[] findByColumn(Class<T> clazz, String column, Serializable... keys) {
|
||||
return findByColumn(clazz, null, column, keys);
|
||||
}
|
||||
@@ -804,7 +821,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param keys
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> T[] findByColumn(Class<T> clazz, final SelectColumn selects, String column, Serializable... keys) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -831,7 +847,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T, V> List<V> queryColumnList(String selectedColumn, Class<T> clazz, String column, Serializable key) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -855,7 +870,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> List<T> queryList(Class<T> clazz, String column, Serializable key) {
|
||||
return queryList(clazz, (SelectColumn) null, column, key);
|
||||
}
|
||||
@@ -870,7 +884,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> List<T> queryList(Class<T> clazz, String column, FilterExpress express, Serializable key) {
|
||||
return queryList(clazz, (SelectColumn) null, column, express, key);
|
||||
}
|
||||
@@ -885,7 +898,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> List<T> queryList(Class<T> clazz, final SelectColumn selects, String column, Serializable key) {
|
||||
return queryList(clazz, selects, column, FilterExpress.EQUAL, key);
|
||||
}
|
||||
@@ -901,7 +913,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> List<T> queryList(Class<T> clazz, final SelectColumn selects, String column, FilterExpress express, Serializable key) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -923,7 +934,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param bean
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> List<T> queryList(final Class<T> clazz, final FilterBean bean) {
|
||||
return queryList(clazz, null, bean);
|
||||
}
|
||||
@@ -937,7 +947,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param bean
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> List<T> queryList(final Class<T> clazz, final SelectColumn selects, final FilterBean bean) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
@@ -962,7 +971,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param bean
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> Sheet<T> querySheet(Class<T> clazz, final Flipper flipper, final FilterBean bean) {
|
||||
return querySheet(clazz, null, flipper, bean);
|
||||
}
|
||||
@@ -977,7 +985,6 @@ final class DataJPASource implements DataSource {
|
||||
* @param bean
|
||||
* @return
|
||||
*/
|
||||
@Override
|
||||
public <T> Sheet<T> querySheet(Class<T> clazz, final SelectColumn selects, final Flipper flipper, final FilterBean bean) {
|
||||
final EntityManager manager = factory.createEntityManager();
|
||||
try {
|
||||
|
||||
@@ -79,7 +79,7 @@ public interface DataSource {
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param ids 主键值
|
||||
* @param ids 主键值
|
||||
*/
|
||||
public <T> void delete(Class<T> clazz, Serializable... ids);
|
||||
|
||||
@@ -94,50 +94,23 @@ public interface DataSource {
|
||||
public <T> void delete(final DataConnection conn, Class<T> clazz, Serializable... ids);
|
||||
|
||||
/**
|
||||
* 根据column字段的值删除对象, 必须是Entity Class
|
||||
* 根据FilterNode的值删除对象, 必须是Entity Class
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param column
|
||||
* @param keys
|
||||
* @param node
|
||||
*/
|
||||
public <T> void deleteByColumn(Class<T> clazz, String column, Serializable... keys);
|
||||
public <T> void delete(Class<T> clazz, FilterNode node);
|
||||
|
||||
/**
|
||||
* 根据column字段的值删除对象, 必须是Entity Class
|
||||
* 根据FilterNode的值删除对象, 必须是Entity Class
|
||||
*
|
||||
* @param <T>
|
||||
* @param conn
|
||||
* @param clazz
|
||||
* @param column
|
||||
* @param keys
|
||||
* @param node
|
||||
*/
|
||||
public <T> void deleteByColumn(final DataConnection conn, Class<T> clazz, String column, Serializable... keys);
|
||||
|
||||
/**
|
||||
* 根据两个column字段的值删除对象, 必须是Entity Class
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param column1
|
||||
* @param key1
|
||||
* @param column2
|
||||
* @param key2
|
||||
*/
|
||||
public <T> void deleteByTwoColumn(Class<T> clazz, String column1, Serializable key1, String column2, Serializable key2);
|
||||
|
||||
/**
|
||||
* 根据两个column字段的值删除对象, 必须是Entity Class
|
||||
*
|
||||
* @param <T>
|
||||
* @param conn
|
||||
* @param clazz
|
||||
* @param column1
|
||||
* @param key1
|
||||
* @param column2
|
||||
* @param key2
|
||||
*/
|
||||
public <T> void deleteByTwoColumn(final DataConnection conn, Class<T> clazz, String column1, Serializable key1, String column2, Serializable key2);
|
||||
public <T> void delete(final DataConnection conn, Class<T> clazz, FilterNode node);
|
||||
|
||||
//------------------------update---------------------------
|
||||
/**
|
||||
@@ -148,13 +121,6 @@ public interface DataSource {
|
||||
*/
|
||||
public <T> void update(T... values);
|
||||
|
||||
/**
|
||||
* 更新对象, 必须是Entity对象
|
||||
*
|
||||
* @param <T>
|
||||
* @param conn
|
||||
* @param values
|
||||
*/
|
||||
public <T> void update(final DataConnection conn, T... values);
|
||||
|
||||
/**
|
||||
@@ -168,17 +134,7 @@ public interface DataSource {
|
||||
*/
|
||||
public <T> void updateColumn(Class<T> clazz, Serializable id, String column, Serializable value);
|
||||
|
||||
/**
|
||||
* 根据主键值更新对象的column对应的值, 必须是Entity Class
|
||||
*
|
||||
* @param <T>
|
||||
* @param conn
|
||||
* @param clazz
|
||||
* @param id
|
||||
* @param column
|
||||
* @param value
|
||||
*/
|
||||
public <T> void updateColumn(final DataConnection conn, Class<T> clazz, Serializable id, String column, Serializable value);
|
||||
public <T> void updateColumn(DataConnection conn, Class<T> clazz, Serializable id, String column, Serializable value);
|
||||
|
||||
/**
|
||||
* 根据主键值给对象的column对应的值+incvalue, 必须是Entity Class
|
||||
@@ -191,16 +147,6 @@ public interface DataSource {
|
||||
*/
|
||||
public <T> void updateColumnIncrement(Class<T> clazz, Serializable id, String column, long incvalue);
|
||||
|
||||
/**
|
||||
* 根据主键值给对象的column对应的值+incvalue, 必须是Entity Class
|
||||
*
|
||||
* @param <T>
|
||||
* @param conn
|
||||
* @param clazz
|
||||
* @param id
|
||||
* @param column
|
||||
* @param incvalue
|
||||
*/
|
||||
public <T> void updateColumnIncrement(final DataConnection conn, Class<T> clazz, Serializable id, String column, long incvalue);
|
||||
|
||||
/**
|
||||
@@ -212,14 +158,6 @@ public interface DataSource {
|
||||
*/
|
||||
public <T> void updateColumns(final T value, final String... columns);
|
||||
|
||||
/**
|
||||
* 更新对象指定的一些字段, 必须是Entity对象
|
||||
*
|
||||
* @param <T>
|
||||
* @param conn
|
||||
* @param value
|
||||
* @param columns
|
||||
*/
|
||||
public <T> void updateColumns(final DataConnection conn, final T value, final String... columns);
|
||||
|
||||
//-----------------------getSingleResult-----------------------------
|
||||
@@ -228,30 +166,42 @@ public interface DataSource {
|
||||
|
||||
public Number getMaxSingleResult(final Class entityClass, final String column, FilterBean bean);
|
||||
|
||||
public Number getMaxSingleResult(final Class entityClass, final String column, FilterNode node);
|
||||
|
||||
//-----------------------------MIN-----------------------------
|
||||
public Number getMinSingleResult(final Class entityClass, final String column);
|
||||
|
||||
public Number getMinSingleResult(final Class entityClass, final String column, FilterBean bean);
|
||||
|
||||
public Number getMinSingleResult(final Class entityClass, final String column, FilterNode node);
|
||||
|
||||
//-----------------------------SUM-----------------------------
|
||||
public Number getSumSingleResult(final Class entityClass, final String column);
|
||||
|
||||
public Number getSumSingleResult(final Class entityClass, final String column, FilterBean bean);
|
||||
|
||||
public Number getSumSingleResult(final Class entityClass, final String column, FilterNode node);
|
||||
|
||||
//----------------------------COUNT----------------------------
|
||||
public Number getCountSingleResult(final Class entityClass);
|
||||
|
||||
public Number getCountSingleResult(final Class entityClass, FilterBean bean);
|
||||
|
||||
public Number getCountSingleResult(final Class entityClass, FilterNode node);
|
||||
|
||||
public Number getCountDistinctSingleResult(final Class entityClass, String column);
|
||||
|
||||
public Number getCountDistinctSingleResult(final Class entityClass, String column, FilterBean bean);
|
||||
|
||||
public Number getCountDistinctSingleResult(final Class entityClass, String column, FilterNode node);
|
||||
|
||||
//-----------------------------AVG-----------------------------
|
||||
public Number getAvgSingleResult(final Class entityClass, final String column);
|
||||
|
||||
public Number getAvgSingleResult(final Class entityClass, final String column, FilterBean bean);
|
||||
|
||||
public Number getAvgSingleResult(final Class entityClass, final String column, FilterNode node);
|
||||
|
||||
//-----------------------find----------------------------
|
||||
/**
|
||||
* 根据主键获取对象
|
||||
@@ -263,93 +213,15 @@ public interface DataSource {
|
||||
*/
|
||||
public <T> T find(Class<T> clazz, Serializable pk);
|
||||
|
||||
/**
|
||||
* 根据主键获取对象
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param selects
|
||||
* @param pk
|
||||
* @return
|
||||
*/
|
||||
public <T> T find(Class<T> clazz, final SelectColumn selects, Serializable pk);
|
||||
|
||||
/**
|
||||
* 根据主键值集合获取对象集合
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param ids
|
||||
* @return
|
||||
*/
|
||||
public <T> T[] find(Class<T> clazz, Serializable... ids);
|
||||
|
||||
/**
|
||||
* 根据唯一索引获取单个对象
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param column
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public <T> T findByColumn(Class<T> clazz, String column, Serializable key);
|
||||
|
||||
/**
|
||||
* 根据两个字段的值获取单个对象
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param column1
|
||||
* @param key1
|
||||
* @param column2
|
||||
* @param key2
|
||||
* @return
|
||||
*/
|
||||
public <T> T findByTwoColumn(Class<T> clazz, String column1, Serializable key1, String column2, Serializable key2);
|
||||
public <T> T find(final Class<T> clazz, final FilterNode node);
|
||||
|
||||
/**
|
||||
* 根据唯一索引获取对象
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param column
|
||||
* @param keys
|
||||
* @return
|
||||
*/
|
||||
public <T> T[] findByColumn(Class<T> clazz, String column, Serializable... keys);
|
||||
|
||||
/**
|
||||
* 根据字段值拉去对象, 对象只填充或排除SelectColumn指定的字段
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param selects 只拉起指定字段名或者排除指定字段名的值
|
||||
* @param column
|
||||
* @param keys
|
||||
* @return
|
||||
*/
|
||||
public <T> T[] findByColumn(Class<T> clazz, final SelectColumn selects, String column, Serializable... keys);
|
||||
|
||||
/**
|
||||
* 根据过滤对象FilterBean查询第一个符合条件的对象
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param bean
|
||||
* @return
|
||||
*/
|
||||
public <T> T find(final Class<T> clazz, final FilterBean bean);
|
||||
|
||||
//-----------------------list set----------------------------
|
||||
public <T> int[] queryColumnIntSet(String selectedColumn, Class<T> clazz, String column, Serializable key);
|
||||
|
||||
public <T> long[] queryColumnLongSet(String selectedColumn, Class<T> clazz, String column, Serializable key);
|
||||
|
||||
public <T> int[] queryColumnIntList(String selectedColumn, Class<T> clazz, String column, Serializable key);
|
||||
|
||||
public <T> long[] queryColumnLongList(String selectedColumn, Class<T> clazz, String column, Serializable key);
|
||||
|
||||
/**
|
||||
* 根据指定字段值查询对象某个字段的集合
|
||||
*
|
||||
@@ -363,54 +235,15 @@ public interface DataSource {
|
||||
*/
|
||||
public <T, V> Set<V> queryColumnSet(String selectedColumn, Class<T> clazz, String column, Serializable key);
|
||||
|
||||
/**
|
||||
* 根据指定字段值查询对象某个字段的集合
|
||||
*
|
||||
* @param <T>
|
||||
* @param <V>
|
||||
* @param selectedColumn
|
||||
* @param clazz
|
||||
* @param column
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public <T, V> Set<V> queryColumnSet(String selectedColumn, Class<T> clazz, FilterNode node);
|
||||
|
||||
public <T, V> Set<V> queryColumnSet(String selectedColumn, Class<T> clazz, FilterBean bean);
|
||||
|
||||
public <T, V> List<V> queryColumnList(String selectedColumn, Class<T> clazz, String column, Serializable key);
|
||||
|
||||
public <T> int[] queryColumnIntSet(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key);
|
||||
public <T, V> List<V> queryColumnList(String selectedColumn, Class<T> clazz, FilterNode node);
|
||||
|
||||
public <T> long[] queryColumnLongSet(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key);
|
||||
|
||||
public <T> int[] queryColumnIntList(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key);
|
||||
|
||||
public <T> long[] queryColumnLongList(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key);
|
||||
|
||||
/**
|
||||
* 根据指定字段值查询对象某个字段的集合
|
||||
*
|
||||
* @param <T>
|
||||
* @param <V>
|
||||
* @param selectedColumn
|
||||
* @param clazz
|
||||
* @param column
|
||||
* @param express
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public <T, V> Set<V> queryColumnSet(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key);
|
||||
|
||||
/**
|
||||
* 根据指定字段值查询对象某个字段的集合
|
||||
*
|
||||
* @param <T>
|
||||
* @param <V>
|
||||
* @param selectedColumn
|
||||
* @param clazz
|
||||
* @param column
|
||||
* @param express
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public <T, V> List<V> queryColumnList(String selectedColumn, Class<T> clazz, String column, FilterExpress express, Serializable key);
|
||||
public <T, V> List<V> queryColumnList(String selectedColumn, Class<T> clazz, FilterBean bean);
|
||||
|
||||
/**
|
||||
* 根据指定字段值查询对象集合
|
||||
@@ -423,63 +256,12 @@ public interface DataSource {
|
||||
*/
|
||||
public <T> List<T> queryList(Class<T> clazz, String column, Serializable key);
|
||||
|
||||
/**
|
||||
* 根据指定字段值查询对象集合
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param column
|
||||
* @param express
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public <T> List<T> queryList(Class<T> clazz, String column, FilterExpress express, Serializable key);
|
||||
public <T> List<T> queryList(final Class<T> clazz, final FilterNode node);
|
||||
|
||||
//-----------------------list----------------------------
|
||||
/**
|
||||
* 根据指定字段值查询对象集合, 对象只填充或排除SelectColumn指定的字段
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param selects
|
||||
* @param column
|
||||
* @param express
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public <T> List<T> queryList(Class<T> clazz, final SelectColumn selects, String column, FilterExpress express, Serializable key);
|
||||
|
||||
/**
|
||||
* 根据指定字段值查询对象集合, 对象只填充或排除SelectColumn指定的字段
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param selects
|
||||
* @param column
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
public <T> List<T> queryList(Class<T> clazz, final SelectColumn selects, String column, Serializable key);
|
||||
|
||||
/**
|
||||
* 根据过滤对象FilterBean查询对象集合
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param bean
|
||||
* @return
|
||||
*/
|
||||
public <T> List<T> queryList(final Class<T> clazz, final FilterBean bean);
|
||||
|
||||
/**
|
||||
* 根据过滤对象FilterBean查询对象集合, 对象只填充或排除SelectColumn指定的字段
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param selects
|
||||
* @param bean
|
||||
* @return
|
||||
*/
|
||||
public <T> List<T> queryList(final Class<T> clazz, final SelectColumn selects, final FilterNode node);
|
||||
|
||||
public <T> List<T> queryList(final Class<T> clazz, final SelectColumn selects, final FilterBean bean);
|
||||
|
||||
//-----------------------sheet----------------------------
|
||||
@@ -496,27 +278,14 @@ public interface DataSource {
|
||||
*/
|
||||
public <T, V> Sheet<V> queryColumnSheet(String selectedColumn, Class<T> clazz, final Flipper flipper, final FilterBean bean);
|
||||
|
||||
/**
|
||||
* 根据过滤对象FilterBean和翻页对象Flipper查询一页的数据
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param flipper
|
||||
* @param bean
|
||||
* @return
|
||||
*/
|
||||
public <T, V> Sheet<V> queryColumnSheet(String selectedColumn, Class<T> clazz, final Flipper flipper, final FilterNode node);
|
||||
|
||||
public <T> Sheet<T> querySheet(Class<T> clazz, final Flipper flipper, final FilterBean bean);
|
||||
|
||||
/**
|
||||
* 根据过滤对象FilterBean和翻页对象Flipper查询一页的数据, 对象只填充或排除SelectColumn指定的字段
|
||||
*
|
||||
* @param <T>
|
||||
* @param clazz
|
||||
* @param selects
|
||||
* @param flipper
|
||||
* @param bean
|
||||
* @return
|
||||
*/
|
||||
public <T> Sheet<T> querySheet(Class<T> clazz, final Flipper flipper, final FilterNode node);
|
||||
|
||||
public <T> Sheet<T> querySheet(Class<T> clazz, final SelectColumn selects, final Flipper flipper, final FilterBean bean);
|
||||
|
||||
public <T> Sheet<T> querySheet(Class<T> clazz, final SelectColumn selects, final Flipper flipper, final FilterNode node);
|
||||
|
||||
}
|
||||
|
||||
@@ -262,7 +262,7 @@ final class EntityCache<T> {
|
||||
this.reproduce.copy(rs, value);
|
||||
}
|
||||
|
||||
public void update(final T value, Attribute<T, Serializable>[] attrs) {
|
||||
public void update(final T value, Collection<Attribute<T, Serializable>> attrs) {
|
||||
if (value == null) return;
|
||||
T rs = this.map.get(this.primary.get(value));
|
||||
if (rs == null) return;
|
||||
|
||||
@@ -143,6 +143,29 @@ public class FilterNode {
|
||||
return rs;
|
||||
}
|
||||
|
||||
protected static <E> String createFilterSQLOrderBy(EntityInfo<E> info, Flipper flipper) {
|
||||
if (flipper == null || flipper.getSort() == null || flipper.getSort().isEmpty()) return "";
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
sb.append(" ORDER BY ");
|
||||
if (info.isNoAlias()) {
|
||||
sb.append(flipper.getSort());
|
||||
} else {
|
||||
boolean flag = false;
|
||||
for (String item : flipper.getSort().split(",")) {
|
||||
if (item.isEmpty()) continue;
|
||||
String[] sub = item.split("\\s+");
|
||||
if (flag) sb.append(',');
|
||||
if (sub.length < 2 || sub[1].equalsIgnoreCase("ASC")) {
|
||||
sb.append("a.").append(info.getSQLColumn(sub[0])).append(" ASC");
|
||||
} else {
|
||||
sb.append("a.").append(info.getSQLColumn(sub[0])).append(" DESC");
|
||||
}
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
private <T> StringBuilder createFilterSQLExpress(final EntityInfo<T> info, Serializable val0) {
|
||||
final StringBuilder val = formatValue(val0);
|
||||
if (val == null) return null;
|
||||
@@ -272,29 +295,6 @@ public class FilterNode {
|
||||
return null;
|
||||
}
|
||||
|
||||
protected static <E> StringBuilder createFilterSQLOrderBy(EntityInfo<E> info, Flipper flipper) {
|
||||
if (flipper == null || flipper.getSort() == null || flipper.getSort().isEmpty()) return null;
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
sb.append(" ORDER BY ");
|
||||
if (info.isNoAlias()) {
|
||||
sb.append(flipper.getSort());
|
||||
} else {
|
||||
boolean flag = false;
|
||||
for (String item : flipper.getSort().split(",")) {
|
||||
if (item.isEmpty()) continue;
|
||||
String[] sub = item.split("\\s+");
|
||||
if (flag) sb.append(',');
|
||||
if (sub.length < 2 || sub[1].equalsIgnoreCase("ASC")) {
|
||||
sb.append("a.").append(info.getSQLColumn(sub[0])).append(" ASC");
|
||||
} else {
|
||||
sb.append("a.").append(info.getSQLColumn(sub[0])).append(" DESC");
|
||||
}
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
return sb;
|
||||
}
|
||||
|
||||
protected static <E> Comparator<E> createFilterComparator(EntityInfo<E> info, Flipper flipper) {
|
||||
if (flipper == null || flipper.getSort() == null || flipper.getSort().isEmpty()) return null;
|
||||
Comparator<E> comparator = null;
|
||||
@@ -320,6 +320,15 @@ public class FilterNode {
|
||||
}
|
||||
|
||||
protected StringBuilder formatValue(Object value) {
|
||||
return formatValue(express, value);
|
||||
}
|
||||
|
||||
protected static String formatToString(Object value) {
|
||||
StringBuilder sb = formatValue(null, value);
|
||||
return sb == null ? null : sb.toString();
|
||||
}
|
||||
|
||||
private static StringBuilder formatValue(FilterExpress express, Object value) {
|
||||
if (value == null) return null;
|
||||
if (value instanceof Number) return new StringBuilder().append(value);
|
||||
if (value instanceof CharSequence) {
|
||||
@@ -344,6 +353,10 @@ public class FilterNode {
|
||||
} else if (value.getClass().isArray()) {
|
||||
int len = Array.getLength(value);
|
||||
if (len == 0) return null;
|
||||
if (len == 1) {
|
||||
Object firstval = Array.get(value, 0);
|
||||
if (firstval != null && firstval.getClass().isArray()) return formatValue(express, firstval);
|
||||
}
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append('(');
|
||||
for (int i = 0; i < len; i++) {
|
||||
|
||||
234
src/com/wentch/redkale/source/JDBCPoolSource.java
Normal file
234
src/com/wentch/redkale/source/JDBCPoolSource.java
Normal file
@@ -0,0 +1,234 @@
|
||||
/*
|
||||
* To change this license header, choose License Headers in Project Properties.
|
||||
* To change this template file, choose Tools | Templates
|
||||
* and open the template in the editor.
|
||||
*/
|
||||
package com.wentch.redkale.source;
|
||||
|
||||
import static com.wentch.redkale.source.DataJDBCSource.*;
|
||||
import java.io.*;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.lang.reflect.Method;
|
||||
import java.nio.file.*;
|
||||
import static java.nio.file.StandardWatchEventKinds.ENTRY_MODIFY;
|
||||
import java.sql.*;
|
||||
import java.util.*;
|
||||
import java.util.concurrent.*;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.logging.Level;
|
||||
import javax.sql.*;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author zhangjx
|
||||
*/
|
||||
public class JDBCPoolSource {
|
||||
|
||||
private static final Map<String, AbstractMap.SimpleEntry<WatchService, List<WeakReference<JDBCPoolSource>>>> maps = new HashMap<>();
|
||||
|
||||
private final AtomicLong usingCounter = new AtomicLong();
|
||||
|
||||
private final AtomicLong creatCounter = new AtomicLong();
|
||||
|
||||
private final AtomicLong cycleCounter = new AtomicLong();
|
||||
|
||||
private final AtomicLong saveCounter = new AtomicLong();
|
||||
|
||||
private final ConnectionPoolDataSource source;
|
||||
|
||||
private final ArrayBlockingQueue<PooledConnection> queue;
|
||||
|
||||
private final ConnectionEventListener listener;
|
||||
|
||||
private final DataJDBCSource dataSource;
|
||||
|
||||
private final String stype; // "" 或 "read" 或 "write"
|
||||
|
||||
private final int max;
|
||||
|
||||
private String url;
|
||||
|
||||
private String user;
|
||||
|
||||
private String password;
|
||||
|
||||
public JDBCPoolSource(DataJDBCSource source, String stype, Properties prop) {
|
||||
this.dataSource = source;
|
||||
this.stype = stype;
|
||||
this.source = createDataSource(prop);
|
||||
this.url = prop.getProperty(JDBC_URL);
|
||||
this.user = prop.getProperty(JDBC_USER);
|
||||
this.password = prop.getProperty(JDBC_PWD);
|
||||
this.max = Integer.decode(prop.getProperty(JDBC_CONNECTIONMAX, "" + Runtime.getRuntime().availableProcessors() * 16));
|
||||
this.queue = new ArrayBlockingQueue<>(this.max);
|
||||
this.listener = new ConnectionEventListener() {
|
||||
|
||||
@Override
|
||||
public void connectionClosed(ConnectionEvent event) {
|
||||
PooledConnection pc = (PooledConnection) event.getSource();
|
||||
if (queue.offer(pc)) saveCounter.incrementAndGet();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void connectionErrorOccurred(ConnectionEvent event) {
|
||||
usingCounter.decrementAndGet();
|
||||
if ("08S01".equals(event.getSQLException().getSQLState())) return; //MySQL特性, 长时间连接没使用会抛出com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
|
||||
dataSource.logger.log(Level.WARNING, "connectionErronOccurred [" + event.getSQLException().getSQLState() + "]", event.getSQLException());
|
||||
}
|
||||
};
|
||||
try {
|
||||
this.watch();
|
||||
} catch (Exception e) {
|
||||
dataSource.logger.log(Level.WARNING, DataSource.class.getSimpleName() + " watch " + dataSource.conf + " error", e);
|
||||
}
|
||||
}
|
||||
|
||||
private void watch() throws IOException {
|
||||
if (dataSource.conf == null || dataSource.name == null) return;
|
||||
final String file = dataSource.conf.getFile();
|
||||
final File f = new File(file);
|
||||
if (!f.isFile() || !f.canRead()) return;
|
||||
synchronized (maps) {
|
||||
AbstractMap.SimpleEntry<WatchService, List<WeakReference<JDBCPoolSource>>> entry = maps.get(file);
|
||||
if (entry != null) {
|
||||
entry.getValue().add(new WeakReference<>(this));
|
||||
return;
|
||||
}
|
||||
final WatchService watcher = f.toPath().getFileSystem().newWatchService();
|
||||
final List<WeakReference<JDBCPoolSource>> list = new CopyOnWriteArrayList<>();
|
||||
Thread watchThread = new Thread() {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
while (!this.isInterrupted()) {
|
||||
final WatchKey key = watcher.take();
|
||||
Thread.sleep(3000); //防止文件正在更新过程中去读取
|
||||
final Map<String, Properties> m = loadProperties(new FileInputStream(file));
|
||||
key.pollEvents().stream().forEach((event) -> {
|
||||
if (event.kind() != ENTRY_MODIFY) return;
|
||||
if (!((Path) event.context()).toFile().getName().equals(f.getName())) return;
|
||||
for (WeakReference<JDBCPoolSource> ref : list) {
|
||||
JDBCPoolSource pool = ref.get();
|
||||
if (pool == null) continue;
|
||||
try {
|
||||
Properties property = m.get(pool.dataSource.name);
|
||||
if (property == null) property = m.get(pool.dataSource.name + "." + pool.stype);
|
||||
if (property != null) pool.change(property);
|
||||
} catch (Exception ex) {
|
||||
dataSource.logger.log(Level.INFO, event.context() + " occur error", ex);
|
||||
}
|
||||
}
|
||||
});
|
||||
key.reset();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
dataSource.logger.log(Level.WARNING, "DataSource watch " + file + " occur error", e);
|
||||
}
|
||||
}
|
||||
};
|
||||
f.getParentFile().toPath().register(watcher, ENTRY_MODIFY);
|
||||
watchThread.setName("DataSource-Watch-" + maps.size() + "-Thread");
|
||||
watchThread.setDaemon(true);
|
||||
watchThread.start();
|
||||
dataSource.logger.log(Level.FINER, watchThread.getName() + " start watching " + file);
|
||||
//-----------------------------------------------------------
|
||||
list.add(new WeakReference<>(this));
|
||||
maps.put(file, new AbstractMap.SimpleEntry<>(watcher, list));
|
||||
}
|
||||
}
|
||||
|
||||
public void change(Properties property) {
|
||||
Method seturlm;
|
||||
Class clazz = source.getClass();
|
||||
String newurl = property.getProperty(JDBC_URL);
|
||||
String newuser = property.getProperty(JDBC_USER);
|
||||
String newpassword = property.getProperty(JDBC_PWD);
|
||||
if (this.url.equals(newurl) && this.user.equals(newuser) && this.password.equals(newpassword)) return;
|
||||
try {
|
||||
try {
|
||||
seturlm = clazz.getMethod("setUrl", String.class);
|
||||
} catch (Exception e) {
|
||||
seturlm = clazz.getMethod("setURL", String.class);
|
||||
}
|
||||
seturlm.invoke(source, newurl);
|
||||
clazz.getMethod("setUser", String.class).invoke(source, newuser);
|
||||
clazz.getMethod("setPassword", String.class).invoke(source, newpassword);
|
||||
this.url = newurl;
|
||||
this.user = newuser;
|
||||
this.password = newpassword;
|
||||
dataSource.logger.log(Level.INFO, DataSource.class.getSimpleName() + "(" + dataSource.name + "." + stype + ") change (" + property + ")");
|
||||
} catch (Exception e) {
|
||||
dataSource.logger.log(Level.SEVERE, DataSource.class.getSimpleName() + " dynamic change JDBC (url userName password) error", e);
|
||||
}
|
||||
}
|
||||
|
||||
public Connection poll() {
|
||||
return poll(0, null);
|
||||
}
|
||||
|
||||
private Connection poll(final int count, SQLException e) {
|
||||
if (count >= 3) {
|
||||
dataSource.logger.log(Level.WARNING, "create pooled connection error", e);
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
PooledConnection result = queue.poll();
|
||||
if (result == null) {
|
||||
if (usingCounter.get() >= max) {
|
||||
try {
|
||||
result = queue.poll(6, TimeUnit.SECONDS);
|
||||
} catch (Exception t) {
|
||||
dataSource.logger.log(Level.WARNING, "take pooled connection error", t);
|
||||
}
|
||||
}
|
||||
if (result == null) {
|
||||
try {
|
||||
result = source.getPooledConnection();
|
||||
result.addConnectionEventListener(listener);
|
||||
usingCounter.incrementAndGet();
|
||||
} catch (SQLException ex) {
|
||||
return poll(count + 1, ex);
|
||||
}
|
||||
creatCounter.incrementAndGet();
|
||||
}
|
||||
} else {
|
||||
cycleCounter.incrementAndGet();
|
||||
}
|
||||
Connection conn;
|
||||
try {
|
||||
conn = result.getConnection();
|
||||
if (!conn.isValid(1)) {
|
||||
dataSource.logger.info("sql connection is not vaild");
|
||||
usingCounter.decrementAndGet();
|
||||
return poll(0, null);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
if (!"08S01".equals(ex.getSQLState())) {//MySQL特性, 长时间连接没使用会抛出com.mysql.jdbc.exceptions.jdbc4.CommunicationsException
|
||||
dataSource.logger.log(Level.FINER, "result.getConnection from pooled connection abort [" + ex.getSQLState() + "]", ex);
|
||||
}
|
||||
return poll(0, null);
|
||||
}
|
||||
return conn;
|
||||
}
|
||||
|
||||
public long getCreatCount() {
|
||||
return creatCounter.longValue();
|
||||
}
|
||||
|
||||
public long getCycleCount() {
|
||||
return cycleCounter.longValue();
|
||||
}
|
||||
|
||||
public long getSaveCount() {
|
||||
return saveCounter.longValue();
|
||||
}
|
||||
|
||||
public void close() {
|
||||
queue.stream().forEach(x -> {
|
||||
try {
|
||||
x.close();
|
||||
} catch (Exception e) {
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user