use of org.neo4j.ogm.session.Neo4jSession in project LinkAgent by shulieTech.
the class Neo4jSessionConstructorInterceptor method afterLast.
@Override
public void afterLast(Advice advice) {
Object[] args = advice.getParameterArray();
Object target = advice.getTarget();
/**
* 压测状态判断
* 影子session回调
*/
if (!PradarSwitcher.isClusterTestEnabled() || target instanceof Neo4JSessionExt) {
return;
}
try {
// 业务库session
Neo4jSession sourceSession = (Neo4jSession) target;
DriverConfiguration driverConfiguration = ((HttpDriver) args[1]).getConfiguration();
String s = driverConfiguration.getURI();
Credentials credentials = driverConfiguration.getCredentials();
String username = null;
if (credentials instanceof AuthTokenCredentials) {
username = ((AuthTokenCredentials) credentials).credentials();
} else if (credentials instanceof UsernamePasswordCredentials) {
username = ((UsernamePasswordCredentials) credentials).getUsername();
}
DataSourceMeta<Neo4jSession> neo4jSessionDataSourceMeta = new DataSourceMeta<Neo4jSession>(s, username, sourceSession);
if (DataSourceWrapUtil.pressureDataSources.containsKey(neo4jSessionDataSourceMeta) && DataSourceWrapUtil.pressureDataSources.get(neo4jSessionDataSourceMeta) != null) {
// 该业务库数据源已经初始化过影子库
return;
}
if (isPerformanceDataSource(driverConfiguration)) {
// 业务库配置是已初始化的影子库
return;
}
// 从应用的影子库配置中获取
String key = DbUrlUtils.getKey(neo4jSessionDataSourceMeta.getUrl(), neo4jSessionDataSourceMeta.getUsername());
if (!GlobalConfig.getInstance().containsShadowDatabaseConfig(key)) {
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子表或影子库!").setDetail("业务库配置:::url: " + s + "; 中间件类型:other").closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
return;
}
MetaData metaData = (MetaData) args[0];
DataSourceWrapUtil.metaDataMap.put(sourceSession, metaData);
DataSourceWrapUtil.wrap(neo4jSessionDataSourceMeta);
} catch (Throwable e) {
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0003").setMessage("影子库配置异常,无法由配置正确生成影子库!").setDetail("url: " + ((HttpDriver) args[1]).getConfiguration().getURI() + Throwables.getStackTraceAsString(e)).closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
throw new PressureMeasureError("Neo4J-002:影子库初始化失败:", e);
}
}
use of org.neo4j.ogm.session.Neo4jSession in project LinkAgent by shulieTech.
the class Neo4jSessionOperationCutOffInterceptor method cutoff0.
@Override
public CutOffResult cutoff0(Advice advice) {
Object[] args = advice.getParameterArray();
String methodName = advice.getBehaviorName();
Object target = advice.getTarget();
/**
* 压测状态为关闭,如果当前为压测流量则直接报错
*/
if (!PradarSwitcher.isClusterTestEnabled()) {
if (Pradar.isClusterTest()) {
throw new PressureMeasureError(PradarSwitcher.PRADAR_SWITCHER_OFF + ":" + AppNameUtils.appName());
}
return CutOffResult.passed();
}
if (!Pradar.isClusterTest()) {
// 非压测流量
return CutOffResult.passed();
}
if (target instanceof Neo4JSessionExt) {
// 影子配置session回调
return CutOffResult.passed();
}
// 避免因配置重新生效而导致使用旧配置
Driver driver1 = Reflect.on(target).get(Neo4JConstants.DYNAMIC_FIELD_DRIVER);
DriverConfiguration configuration = driver1.getConfiguration();
String uri = configuration.getURI();
Credentials credentials = configuration.getCredentials();
String username = null;
if (credentials instanceof AuthTokenCredentials) {
username = ((AuthTokenCredentials) credentials).credentials();
} else if (credentials instanceof UsernamePasswordCredentials) {
username = ((UsernamePasswordCredentials) credentials).getUsername();
}
DataSourceMeta<Neo4jSession> neo4jSessionDataSourceMeta = new DataSourceMeta<Neo4jSession>(uri, username, (Neo4jSession) target);
DataSourceWrapUtil.wrap(neo4jSessionDataSourceMeta);
// 压测流量转发影子库
if (!DataSourceWrapUtil.pressureDataSources.containsKey(neo4jSessionDataSourceMeta) || null == DataSourceWrapUtil.pressureDataSources.get(neo4jSessionDataSourceMeta)) {
ErrorReporter.buildError().setErrorType(ErrorTypeEnum.DataSource).setErrorCode("datasource-0002").setMessage("没有配置对应的影子库!").setDetail("Neo4J").closePradar(ConfigNames.SHADOW_DATABASE_CONFIGS).report();
throw new PressureMeasureError("Neo4J-002:影子库配置不存在!");
}
DbMediatorDataSource<?> dbMediatorDataSource = DataSourceWrapUtil.pressureDataSources.get(neo4jSessionDataSourceMeta);
Neo4JSessionExt dataSourcePerformanceTest = (Neo4JSessionExt) dbMediatorDataSource.getDataSourcePerformanceTest();
Operation operation = Neo4JSessionOperation.of(methodName);
PradarSwitcher.httpPassPrefix.set(dataSourcePerformanceTest.getDriver().getConfiguration().getURI());
return CutOffResult.cutoff(operation.invoke(dataSourcePerformanceTest, args));
}
use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.
the class PizzaIntegrationTest method shouldBeAbleToModifyPropertiesAndRelsWithinSingleSave.
@Test
public void shouldBeAbleToModifyPropertiesAndRelsWithinSingleSave() {
Crust crust = new Crust("Thin Crust");
Topping pepperoni = new Topping("Pepperoni");
final ArrayList<Topping> toppings = new ArrayList<>();
toppings.add(pepperoni);
Pizza pizza = new Pizza("Godfather", crust, toppings);
session.save(pizza);
Topping mushroom = new Topping("Mushroom");
session.save(pepperoni);
session.save(mushroom);
session.clear();
Long id = pizza.getId();
assertThat(id).isNotNull();
// detach the pizza
Session session2 = sessionFactory.openSession();
// NOTE: if we instead reload into our pizza object, the test will pass, but what
// this does is create a new object in the session. And, rather than work with
// the new attached object 'loadedPizza', we continue to work with the detached object, 'pizza'.
// this is the first condition for failure.
Pizza loadedPizza = session2.load(Pizza.class, id);
// now we create a relationship and update a property on the detached pizza object.
// note that we don't save the Crust first. Crust is a new object when pizza is saved, so
// we will generate a 2-statement cypher request. This is the second condition for failure
loadedPizza.setName("Just bread");
loadedPizza.getToppings().clear();
Topping pepperoniTopping = session2.load(Topping.class, pepperoni.getId());
loadedPizza.getToppings().add(pepperoniTopping);
Topping mushroomTopping = session2.load(Topping.class, pepperoni.getId());
loadedPizza.getToppings().add(mushroomTopping);
// pizza should be dirty
assertThat(((Neo4jSession) session2).context().isDirty(loadedPizza)).isTrue();
session2.save(loadedPizza);
// pizza should NOT be dirty - but it is, indicating it's not current in the cache.
// this should pass
assertThat(((Neo4jSession) session2).context().isDirty(loadedPizza)).isFalse();
}
use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.
the class MovieTest method testDeserialiseMovie.
@Test
public void testDeserialiseMovie() {
MetaData metadata = new MetaData("org.neo4j.ogm.domain.cineasts.annotated");
Neo4jSession session = new Neo4jSession(metadata, true, new MoviesRequest());
Movie movie = session.load(Movie.class, UUID.fromString("38ebe777-bc85-4810-8217-096f29a361f1"), 1);
assertThat(movie.getTitle()).isEqualTo("Pulp Fiction");
assertThat(movie.getRatings()).isNotNull();
assertThat(movie.getRatings()).hasSize(1);
Rating rating = movie.getRatings().iterator().next();
assertThat(rating.getUser().getName()).isEqualTo("Michal");
assertThat(rating.getMovie().getTitle()).isEqualTo("Pulp Fiction");
}
use of org.neo4j.ogm.session.Neo4jSession in project neo4j-ogm by neo4j.
the class BasicDriverTest method shouldSaveMultipleObjectsWithWriteProtection.
@Test
public void shouldSaveMultipleObjectsWithWriteProtection() throws Exception {
User bilbo = new User("Bilbo Baggins");
session.save(bilbo);
session.clear();
try {
((Neo4jSession) session).addWriteProtection(WriteProtectionTarget.PROPERTIES, object -> (object instanceof User) && bilbo.getId().equals(((User) object).getId()));
User frodo = new User("Frodo Beutlin");
bilbo.befriend(frodo);
bilbo.setName("The wrong name");
// Get an Iterable which is not a Collection
Iterable<User> iterable = Stream.of(bilbo, frodo)::iterator;
assertThat(iterable).isNotInstanceOf(Collection.class);
session.save(iterable);
session.clear();
Collection<User> users = session.loadAll(User.class);
assertThat(users).hasSize(2).extracting(User::getName).containsExactlyInAnyOrder("Bilbo Baggins", "Frodo Beutlin");
} finally {
((Neo4jSession) session).removeWriteProtection(WriteProtectionTarget.PROPERTIES);
}
}
Aggregations